{"id":7446,"date":"2026-03-08T00:01:14","date_gmt":"2026-03-08T08:01:14","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7446"},"modified":"2026-03-14T10:04:31","modified_gmt":"2026-03-14T17:04:31","slug":"what-is-the-largest-value-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7446","title":{"rendered":"What is the Largest Value &#8211; Solution"},"content":{"rendered":"<p>Once again, the challenge for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7424\">this month&#8217;s exercise<\/a> is to unravel a math puzzle. This time, the puzzle reads like this:<br \/>\n<!--more--><br \/>\nFind all positive values a+b+c+d = 36. Then determine which is the largest value generated for ab+bc+cd?<\/p>\n<p>My first approach was to figure all the permutations of four values that add up to 36. In other words, not to repeat any already-tested combinations. Such code would be delightful, but also a pain. No, my solution was just to brute-force it with a series of <em>for<\/em> loops and then a &#8220;max&#8221; test for results that total to 36:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_03-Exercise.c\" rel=\"noopener\" target=\"_blank\">2026_03-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int a,b,c,d,max;\r\n\r\n    max = 0;\r\n    for( a=0; a&lt;36; a++ )\r\n        for( b=0; b&lt;36; b++ )\r\n            for( c=0; c&lt;36; c++ )\r\n                for( d=0; d&lt;36; d++ )\r\n                    if( a+b+c+d == 36 )\r\n                        if( (a*b)+(b*c)+(c*d) &gt; max )\r\n                            max = (a*b)+(b*c)+(c*d);\r\n    printf(\"%d\\n\",max);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Nested <em>for<\/em> loops cycle variables <code>a<\/code>, <code>b<\/code>, <code>c<\/code>, and <code>d<\/code> from 0 through 35. This approach is very brute-forcey, but it works.<\/p>\n<p>An <em>if<\/em> statement tests to see whether the accumulated values total to 36:<\/p>\n<p><code>if( a+b+c+d == 36 )<\/code><\/p>\n<p>When true, a second <em>if<\/em> statement calculates the value of ab+bc+cd and compares it with the current value of variable <code>max<\/code>:<\/p>\n<p><code>if( (a*b)+(b*c)+(c*d) &gt; max )<\/code><\/p>\n<p>When the value calculated is greater than max, the value of <code>max<\/code> is reset:<\/p>\n<p><code>max = (a*b)+(b*c)+(c*d);<\/code><\/p>\n<p>After running through all the possibilities, the value of <code>max<\/code> represents the largest value calculated, which is 324.<\/p>\n<p>I just recognized that this code consists of three, not two statements as I wrote in the original post. The first statement initializes variable <code>max<\/code>. But the nested <em>for<\/em> loops and their <em>if<\/em> tests are all a single statement:<\/p>\n<pre>for( a=0; a&lt;36; a++ )\r\n    for( b=0; b&lt;36; b++ )\r\n        for( c=0; c&lt;36; c++ )\r\n            for( d=0; d&lt;36; d++ )\r\n                if( a+b+c+d == 36 )\r\n                    if( (a*b)+(b*c)+(c*d) &gt; max )\r\n                        max = (a*b)+(b*c)+(c*d);<\/pre>\n<p>There&#8217;s only a single semicolon in the stack, which makes it all a single statement.<\/p>\n<p>The final statement is the <em>printf()<\/em> to output the result.<\/p>\n<p>I hope that your solution met with success. As I wrote above, it would be more efficient to work through the values and avoid repeating already tested results. But the brute force method works and the code runs just as fast. Still, I may attempt to address non-repeating solutions in a future lesson.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Once again, the challenge for this month&#8217;s exercise is to unravel a math puzzle. This time, the puzzle reads like this:<\/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-7446","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\/7446","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=7446"}],"version-history":[{"count":2,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7446\/revisions"}],"predecessor-version":[{"id":7453,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7446\/revisions\/7453"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7446"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}