{"id":6265,"date":"2024-03-01T00:01:33","date_gmt":"2024-03-01T08:01:33","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6265"},"modified":"2024-03-08T13:09:55","modified_gmt":"2024-03-08T21:09:55","slug":"calculating-the-subfactorial","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6265","title":{"rendered":"Calculating the Subfactorial"},"content":{"rendered":"<h2>Difficulty: &#9733; &#9733; &#9733; &#9733;<\/h2>\n<p>Calculating a factorial is a common programming exercise, often coupled with introducing the concept of recursion. Mathematically, the factorial has a twin: the subfactorial. Its calculation can be a fun programming challenge, which is the subject of this month&#8217;s Exercise.<br \/>\n<!--more--><br \/>\nAs a review, a factorial is a value written as <em>n<\/em>!. The value represents integer <em>n<\/em> multiplied sequentially. For example, 5! is 1&#215;2&#215;3&#215;4&#215;5, which equals 120. This value is the number of permutations possible with five items.<\/p>\n<p>Figure 1 illustrates 3! or three factorial. It shows the number of ways to arrange three objects, which is what 3! represents.<\/p>\n<div id=\"attachment_6269\" style=\"width: 153px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6269\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2024\/03\/0301-figure1-143x300.png\" alt=\"Three factorial\" width=\"143\" height=\"300\" class=\"size-medium wp-image-6269\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2024\/03\/0301-figure1-143x300.png 143w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2024\/03\/0301-figure1.png 290w\" sizes=\"auto, (max-width: 143px) 100vw, 143px\" \/><p id=\"caption-attachment-6269\" class=\"wp-caption-text\">Figure 1. The value 3! shows how many ways to arrange three objects. 3! = 6 different arrangements.<\/p><\/div>\n<p>The subfactorial works along similar lines. It&#8217;s written with the exclamation point <em>before<\/em> the value: subfactorial three is !3. The subfactorial limits the arrangements so that no objects can be in the same position as in the original. Value !3 calls out all non-unique arrangements for three objects. An example is shown in Figure 2.<\/p>\n<div id=\"attachment_6270\" style=\"width: 171px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6270\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2024\/03\/0301-figure2-161x300.png\" alt=\"\" width=\"161\" height=\"300\" class=\"size-medium wp-image-6270\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2024\/03\/0301-figure2-161x300.png 161w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2024\/03\/0301-figure2.png 309w\" sizes=\"auto, (max-width: 161px) 100vw, 161px\" \/><p id=\"caption-attachment-6270\" class=\"wp-caption-text\">Figure 2. Three subfactorial (!3) shows that only two arrangements are possible when compared with the original. !3 = 2.<\/p><\/div>\n<p>The best explanation of the subfactorial process, also called <em>derangement<\/em>, is found in <a href=\"https:\/\/www.youtube.com\/watch?v=UfwJH7l3ai4\" rel=\"noopener\" target=\"_blank\">this video<\/a> from Andy Math. Do take time to watch it, as it describes a formula you can use to calculate subfactorials. The formula is illustrated in Figure 3.<\/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 3. The equation for calculating a subfactorial.<\/p><\/div>\n<p>Your task for this month&#8217;s Exercise is to write code that outputs values for subfactorials !0 through !13. You can use the equation from Figure 3 as your inspiration, which is what I did. You can also check out the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Derangement\" rel=\"noopener\" target=\"_blank\">Derangement Wikipedia page<\/a>, which offers more formulas and mathematical mumbojumbo.<\/p>\n<p>Here is output from my solution, the list of subfactorial values for !0 through !13:<\/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>As with factorials, these subfactorials are known values, though you can&#8217;t just write a solution that outputs the results shown above. No, you must code a solution in C.<\/p>\n<p>Try this Exercise on your own before you check out <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6283\">my solution<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Also known as &#8220;derangement.&#8221; Have fun with that. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6265\">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-6265","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\/6265","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=6265"}],"version-history":[{"count":15,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6265\/revisions"}],"predecessor-version":[{"id":6299,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6265\/revisions\/6299"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}