{"id":6283,"date":"2024-03-08T00:01:08","date_gmt":"2024-03-08T08:01:08","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6283"},"modified":"2024-02-24T12:50:02","modified_gmt":"2024-02-24T20:50:02","slug":"calculating-the-subfactorial-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6283","title":{"rendered":"Calculating the Subfactorial &#8211; Solution"},"content":{"rendered":"<p>Do you feel adequately deranged working <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6265\">this month&#8217;s Exercise<\/a>? I&#8217;m more of a math fanboy than an expert, yet I enjoyed the process of coding a subfactorial based in the equation presented.<br \/>\n<!--more--><br \/>\nMy solution does require a recursive function to determine a factorial; factorials are needed when calculating a subfactorial. But my <em>derange()<\/em> function need not be recursive. It&#8217;s sequential, which is obvious when looking at the formula shown in Figure 1.<\/p>\n<div id=\"attachment_6274\" style=\"width: 460px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6274\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2024\/03\/0301-figure3.png\" alt=\"\" width=\"450\" height=\"266\" class=\"size-full wp-image-6274\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2024\/03\/0301-figure3.png 450w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2024\/03\/0301-figure3-300x177.png 300w\" sizes=\"auto, (max-width: 450px) 100vw, 450px\" \/><p id=\"caption-attachment-6274\" class=\"wp-caption-text\">Figure 1. The equation for calculating a subfactorial.<\/p><\/div>\n<p>Recursive functions work well for continued fractions, but the equation in Figure 1 is finite. A loop is sufficient to plow through the values and arrive at the proper subfactorial value. Here is my solution:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_03-Exercise.c\" rel=\"noopener\" target=\"_blank\">2024_03-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nlong factorial(long f)\r\n{\r\n    if( f&lt;=1 )\r\n        return(1);\r\n    else\r\n        return(f*factorial(f-1));\r\n}\r\n\r\nlong derange(long d)\r\n{\r\n    long r,t;\r\n\r\n    r = 0;\r\n    t = factorial(d);\r\n    while(d&gt;=0)\r\n    {\r\n        r += (d%2 ? -1 : 1)*t\/factorial(d);\r\n        d--;\r\n    }\r\n\r\n    return(r);\r\n}\r\n\r\nint main()\r\n{\r\n    long a;\r\n\r\n    for(a=0; a&lt;14; a++)\r\n        printf(\"!%ld = %ld\\n\",a,derange(a));\r\n\r\n    return 0;\r\n}<\/pre>\n<p>My code uses <em>long<\/em> integers throughout. Yes, I originally used the <em>int<\/em> data type, but it&#8217;s too narrow to obtain the larger values.<\/p>\n<p>The <em>main()<\/em> function loops through values zero through 13, calling the <em>derange()<\/em> function in a <em>printf()<\/em> statement.<\/p>\n<p>The <em>derange()<\/em> function obtains the factorial of the value passed, <code>d<\/code>. Then a <em>while<\/em> loop works through the equation flipping between <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6224\">positive and negative values<\/a> multiplied by the factorial of the original value of <code>d<\/code> and divided by the current factorial of <code>d<\/code>. This expression is how I interpret the equation from Figure 1. The result is returned in variable <code>r<\/code>.<\/p>\n<p>The <em>factorial()<\/em> function is recursive, mirroring <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1080\">a post<\/a> I wrote a while back. I did update the code so that the <em>if<\/em> test evaluates <code>f<=1<\/code>, which accounts for !0.<\/p>\n<p>Here is a sample run:<\/p>\n<p><code>!0 = 1<br \/>\n!1 = 0<br \/>\n!2 = 1<br \/>\n!3 = 2<br \/>\n!4 = 9<br \/>\n!5 = 44<br \/>\n!6 = 265<br \/>\n!7 = 1854<br \/>\n!8 = 14833<br \/>\n!9 = 133496<br \/>\n!10 = 1334961<br \/>\n!11 = 14684570<br \/>\n!12 = 176214841<br \/>\n!13 = 2290792932<\/code><\/p>\n<p>These are the values you must see to gauge your solution's success.<\/p>\n<p>My solution is only one approach, and the equation I use is only one way to reveal subfactorials. I hope your solution is successful, but that you also enjoyed coding it as much as I did mine.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Do you feel adequately deranged working this month&#8217;s Exercise? I&#8217;m more of a math fanboy than an expert, yet I enjoyed the process of coding a subfactorial based in the equation presented.<\/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-6283","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\/6283","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=6283"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6283\/revisions"}],"predecessor-version":[{"id":6291,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6283\/revisions\/6291"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}