{"id":4545,"date":"2021-01-08T00:01:49","date_gmt":"2021-01-08T08:01:49","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4545"},"modified":"2021-01-08T09:02:48","modified_gmt":"2021-01-08T17:02:48","slug":"find-the-most-factors-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4545","title":{"rendered":"Find the Most Factors &#8211; Solution"},"content":{"rendered":"<p>The challenge for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4537\">this month&#8217;s Exercise<\/a> is to discover those values in the range 1 to 999 that have the highest number of factors. Never mind the math! Let C and the computer do the work for you.<br \/>\n<!--more--><br \/>\nI devised a solution in three parts:<\/p>\n<p>First, determine the factors of values from 1 through 999. This loop was borrowed from my earlier <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4413\">prime number example<\/a>. The big difference is that the factors for each value is stored in an array, <code>factors[]<\/code>:<\/p>\n<p><code>factors[v]++;<\/code><\/p>\n<p>Variable <code>v<\/code> represents the value examined, which is tied to an element in the <code>factors[]<\/code> array. This approach makes sense when you study the earlier code example.<\/p>\n<p>Second, I find the maximum (largest) value in the <code>factors[]<\/code> array. This part of the code uses a find-the-maximum-value algorithm. Further, I&#8217;m interested only in the value, not the element number. The value is saved in variable <code>max<\/code>.<\/p>\n<p>Third and finally, a loop plows through all elements in the <code>factors[]<\/code> array to see which match the <code>max<\/code> value. This way, should multiple values have the same number of factors, all are output.<\/p>\n<p>Here&#8217;s my solution:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_01-Exercise.c\" rel=\"noopener\" target=\"_blank\">2020_01-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n#define SIZE 1000\r\n\r\nint main()\r\n{\r\n    int factors[SIZE];\r\n    int v,x,max;\r\n\r\n    <span class=\"comments\">\/* determine the factors *\/<\/span>\r\n    v = 1;\r\n    while(v&lt;SIZE)\r\n    {\r\n        factors[v] = 1;\r\n        for( x=1; x&lt;v; x++ )\r\n        {\r\n            if( v%x == 0 )\r\n            {\r\n                factors[v]++;\r\n            }\r\n        }\r\n        v++;\r\n    }\r\n\r\n    <span class=\"comments\">\/* obtain the greatest factor *\/<\/span>\r\n    max = 0;\r\n    for( x=1; x&lt;SIZE; x++ )\r\n    {\r\n        if( factors[x]&gt;max )\r\n            max = factors[x];\r\n    }\r\n\r\n    <span class=\"comments\">\/* Show the highest factors *\/<\/span>\r\n    <span class=\"comments\">\/* This also accounts for multiple values *\/<\/span>\r\n    for( x=1; x&lt;SIZE; x++ )\r\n    {\r\n        if( factors[x]==max )\r\n            printf(\"%d has %d factors\\n\",x,max);\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The highest number of factors is determined by running the code. The winner is:<\/p>\n<p><code>840 has 32 factors<\/code><\/p>\n<p>Impressive &mdash; and not what I expected, but it checks out. I figured multiple values would have a high number of factors, but the winner is 840.<\/p>\n<p>The code can be improved upon, naturally. If I knew maths better than I do, I&#8217;m sure some algorithms or rules exist for eliminating some numbers as impossible factors. Pulling them out of the factor loop could optimize the code. Other approaches exist to solve the problem, which is true of all programming.<\/p>\n<p>If your solution worked and yielded the same result, great! I find programming exercises fun not only to work out a problem (and the solution may come in handy later), but also to discover the multiple ways that a single task can be implemented.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The challenge for this month&#8217;s Exercise is to discover those values in the range 1 to 999 that have the highest number of factors. Never mind the math! Let C and the computer do the work for you.<\/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-4545","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\/4545","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=4545"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4545\/revisions"}],"predecessor-version":[{"id":4573,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4545\/revisions\/4573"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4545"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4545"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4545"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}