{"id":6701,"date":"2024-12-08T00:01:17","date_gmt":"2024-12-08T08:01:17","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6701"},"modified":"2024-12-07T09:44:36","modified_gmt":"2024-12-07T17:44:36","slug":"the-double-factorial-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6701","title":{"rendered":"The Double Factorial &#8211; Solution"},"content":{"rendered":"<p>This <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6688\">month&#8217;s Exercise<\/a> is to write code to calculate a double factorial, which uses the !! notation. A double factorial works like a factorial, but uses only odd or even values based on the parity of the starting value.<br \/>\n<!--more--><br \/>\nI wrote my first solution as a loop. The loop tallies the factors of the starting value minus two for each spin of the loop. It repeats as long as the original value, <code>f<\/code>, is greater than 2:<\/p>\n<p><code>while(f&gt;2)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;t *= (f-=2);<\/code><\/p>\n<p>The double factorial value is stored in variable <code>t<\/code>, which is initialized to <code>f<\/code> before the loop begins. Here is the full code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_12-Exercise-a.c\" rel=\"noopener\" target=\"_blank\">2024_12-Exercise-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int f,t;\r\n\r\n    <span class=\"comments\">\/* prompt *\/<\/span>\r\n    printf(\"Enter a positive integer: \");\r\n    scanf(\"%d\",&amp;f);\r\n    if( f&lt;1 ) return -1;\r\n\r\n    <span class=\"comments\">\/* output results *\/<\/span>\r\n    printf(\"%d!! = \",f);\r\n    t = f;\r\n    while(f&gt;2)\r\n        t *= (f-=2);\r\n    printf(\"%d\\n\",t);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The expression <code>t *= (f-=2);<\/code> works regardless of whether the original value is odd or even. In either case, the value is reduced by two every spin of the loop. When it&#8217;s less than 2, the loop stops. Here&#8217;s a sample run:<\/p>\n<p><code>Enter a positive integer: 8<br \/>\n8!! = 384<\/code><\/p>\n<p>In the original post, I mentioned how recursion is often used to calculate a factorial. In fact, factorials are a great tool to use to demonstrate recursion. So, I sat back, looked at my first solution (above), and challenged myself to code a recursive version.<\/p>\n<p>Recursion requires a recursive function to call. The puzzle is how to set the expression <code>t *= (f-=2);<\/code> within the recursive function, not to mention how to write the thing so that it properly unwinds. Here&#8217;s what I came up with:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_12-Exercise-b.c\" rel=\"noopener\" target=\"_blank\">2024_12-Exercise-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint double_factorial(int a)\r\n{\r\n    if( a&gt;2 )\r\n        return( a * double_factorial(a-2) );\r\n    return(a);\r\n}\r\n\r\nint main()\r\n{\r\n    int f;\r\n\r\n    <span class=\"comments\">\/* prompt *\/<\/span>\r\n    printf(\"Enter a positive integer: \");\r\n    scanf(\"%d\",&amp;f);\r\n    if( f&lt;1 ) return -1;\r\n\r\n    <span class=\"comments\">\/* output results *\/<\/span>\r\n    printf(\"%d!! = %d\\n\",f,double_factorial(f) );\r\n\r\n    return 0;\r\n}<\/pre>\n<p>In the <em>double_factorial()<\/em> function, the recursion continues as long as the value passed (variable <code>a<\/code>) is greater than 2. The expression fits into a <em>return<\/em> statement, so the calculations are done (and returned) when the function unwinds. The final <em>return<\/em> statement passes back the result &mdash; which works!<\/p>\n<p>This solution surprised me because I wrote it as presented without really thinking about it. Normally I suffer through some mental anguish to ensure that recursion doesn&#8217;t blow up. But this time I just wrote it, crossed my fingers, and built the code. My success might mean that I&#8217;m so used to writing recursive functions that I&#8217;m just good at it or &mdash; more likely &mdash; I got lucky. Either way, the code works and I am pleased.<\/p>\n<p>Factorials are often used in probability and statistics. I don&#8217;t know where double factorials fit into reality because I couldn&#8217;t understand most of the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Double_factorial\" target=\"_blank\">Wikipedia page<\/a> on the topic. Still, the goal here is to code a program to output a double factorial. You solution may not look exactly as mine does, it need only run and output the proper result.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This month&#8217;s Exercise is to write code to calculate a double factorial, which uses the !! notation. A double factorial works like a factorial, but uses only odd or even values based on the parity of the starting value.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-6701","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6701","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6701"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6701\/revisions"}],"predecessor-version":[{"id":6716,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6701\/revisions\/6716"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}