{"id":3162,"date":"2018-06-30T00:01:20","date_gmt":"2018-06-30T07:01:20","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=3162"},"modified":"2018-06-23T11:39:46","modified_gmt":"2018-06-23T18:39:46","slug":"factors","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3162","title":{"rendered":"Factors"},"content":{"rendered":"<p>Factor is a scary word, but not really a mathematical monstrosity. If you have an integer value, its factors are the other integers that can divide evenly into the original value. The computer can deftly discover these values, providing you give it the proper C code to munch upon.<br \/>\n<!--more--><br \/>\nWhen I boned-up on this topic, I thought of money. Yes, I think of money often, but in this case I thought of making change.<\/p>\n<p>For example, currency denominations are all factors of higher values: A $20 bill can be broken into 20 $1s or 4 $5s or two $10s. The values 1, 2, 4, 5, and 10 are all factors of 20. The value 20 is also a factor of 20. Incidentally, all numbers have their own value and 1 as factors. A prime number has <em>only<\/em> its own value and 1 as factors.<\/p>\n<p>Yes, all this mathematical stuff can get nauseating quite quickly. For coding, however, calculating the factors of an integer presents an interesting puzzle.<\/p>\n<p>One solution I saw had about 150 lines of code and three functions to do the job. Oh, and it used pointers. &#8220;Rubbish!&#8221; I thought, as I set upon to code my own factoring program.<\/p>\n<p>To handle the math, I employed the modulus operator, <code>%<\/code>. It returns zero when two integers divide evenly into each other. That&#8217;s pretty much the golden ticket I need to determine whether variable <code>x<\/code> is a factor of integer <code>f<\/code>:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int x;\r\n    unsigned f;\r\n\r\n    printf(\"Enter a positive integer: \");\r\n    scanf(\"%u\",&amp;f);\r\n\r\n    printf(\"Factors of %d:\\n\",f);\r\n    for(x=1;x&lt;=f;x++)\r\n        if( f % x == 0)\r\n            printf(\"%d\\n\",x);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The program prompts the user to enter a positive (<em>unsigned<\/em>) integer value in Lines 8 and 9. A <em>for<\/em> loop at Line 12 plows through the values 1 through variable <code>f<\/code>. If the result of <code>f % x<\/code> is equal to zero, the value is output.<\/p>\n<p>Here&#8217;s a sample run for the value 56:<\/p>\n<pre><code>Enter a positive integer: 56\r\nFactors of 56:\r\n1\r\n2\r\n4\r\n7\r\n8\r\n14\r\n28\r\n56<\/code><\/pre>\n<p>Each of the values output divides evenly into 56.<\/p>\n<p>Here&#8217;s output for a prime number:<\/p>\n<pre><code>Enter a positive integer: 101\r\nFactors of 101:\r\n1\r\n101<\/code><\/pre>\n<p>I tried the code on some humongous values and it ran just fine, though as the values increase, the code runs more slowly. That&#8217;s to be expected. Still, I don&#8217;t understand why the other example I saw used so many lines of code and different functions to do the same thing?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yet another math problem that can be solved by a programmer who doesn&#8217;t really know much math. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3162\">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":[2],"tags":[],"class_list":["post-3162","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3162","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=3162"}],"version-history":[{"count":2,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3162\/revisions"}],"predecessor-version":[{"id":3172,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3162\/revisions\/3172"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}