{"id":6688,"date":"2024-12-01T00:01:30","date_gmt":"2024-12-01T08:01:30","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6688"},"modified":"2024-12-08T08:21:00","modified_gmt":"2024-12-08T16:21:00","slug":"the-double-factorial","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6688","title":{"rendered":"The Double Factorial"},"content":{"rendered":"<h2>Difficulty: &#9733; &#9733; &#9734; &#9734;<\/h2>\n<p>It&#8217;s common in programming to use a factorial as a way to teach about recursion. For example, 5! (five factorial) is 5&times;4&times;3&times;2&times;1, which is 120. Calculating this result can be done with a recursive function, which is practical and satisfying. But mathematicians have a twisted sense of humor, so they invented something called a double factorial.<br \/>\n<!--more--><br \/>\nA <a href=\"https:\/\/en.wikipedia.org\/wiki\/Double_factorial\" rel=\"noopener\" target=\"_blank\">double factorial<\/a> works like a factorial, but the results are based on parity of the original value, odd or even. The calculation still involves descending values, just not all of them.<\/p>\n<p>For example: 5!! (five double-factorial) is equal to 5&times;3&times;1, which is 15. Because five is an odd number, only odd values are used to calculate the result. Values 4 and 2 are skipped.<\/p>\n<p>Here&#8217;s an even parity double factorial calculation:<\/p>\n<p>8!! = 8&times;6&times;4&times;2 = 384<\/p>\n<p>Only even values are used to obtain the result. Values 7, 5, and 3 are skipped. (Obviously, the final value, one, has no impact on the result.)<\/p>\n<p>When I first encountered the double factorial, I was surprised that the results are less than a single factorial; &#8220;double&#8221; implies larger, right? Yet, this relationship means that <em>n<\/em>! &gt; <em>n<\/em>!!. I&#8217;m uncertain what bearing this effect has, though it&#8217;s interesting to note that mathematically <em>n<\/em>! = <em>n<\/em>!! * (<em>n<\/em>-1)!!<\/p>\n<p>For example: 5! = 120. The result of 5!! is 15 and the result of 4!! = 8. Multiply the results together, 15&times;8, and you get 120, which is 5!.<\/p>\n<p>Enough math!<\/p>\n<p>Your task for this month&#8217;s Exercise is to write code that calculates the double factorial of a positive integer. Prompt the use to input a positive integer value. Output the result.<\/p>\n<p>Here&#8217;s a sample run from my solution:<\/p>\n<p><code>Enter a positive integer: 9<br \/>\n9!! = 945<\/code><\/p>\n<p>To confirm your result, you can visit the <a href=\"https:\/\/www.calculatorsoup.com\/calculators\/discretemathematics\/multifactorials.php\" rel=\"noopener\" target=\"_blank\">Calculator Soup website<\/a>, which features a double factorial calculator.<\/p>\n<p>Please try this challenge on your own before you sneak a look at <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6701\">my solutions<\/a>. One uses a loop and the other uses recursion to calculate the double factorial.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just when you think the mathematicians are done, they come up with something else. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6688\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-6688","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6688","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=6688"}],"version-history":[{"count":9,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6688\/revisions"}],"predecessor-version":[{"id":6763,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6688\/revisions\/6763"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6688"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6688"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6688"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}