{"id":4470,"date":"2020-11-28T00:01:10","date_gmt":"2020-11-28T08:01:10","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4470"},"modified":"2020-12-05T08:33:00","modified_gmt":"2020-12-05T16:33:00","slug":"the-chicken-mcnuggets-problem","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4470","title":{"rendered":"The Chicken McNuggets Problem"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/www.mcdonalds.com\/is\/image\/content\/dam\/uk\/nfl\/nutrition\/nfl-product\/product\/products\/mcdonalds-20-Chicken-McNuggets-ShareBox.jpg\" alt=\"Box of 20 Chicken McNuggets, McDonalds\" \/><br \/>\nChicken McNuggets come several to a box, depending on what you order: six pieces for a kid, nine pieces for an adult, or twenty pieces for an honest adult. These numbers in various combinations form what the math nerds call McNugget Numbers.<br \/>\n<!--more--><br \/>\nYes, leave it up to the math nerds to take something delightful and turn it into a number game. Or, in this case, a programming puzzle.<\/p>\n<p>The way this puzzle works is that any value evenly divisible by a McNuggets quantity (6, 9, 20) is a McNuggets number. Right away, 6, 9, and 20 are McNuggets numbers. But so are 12 (6+6) and 15 (6+9) and 44 (20+6+6+6+6). Any value that isn&#8217;t a combination of 6, 9, and\/or 20 is a non-McNuggets number.<\/p>\n<p>Using the Power Of The Computer, it&#8217;s possible to determine which values are McNuggets numbers and which aren&#8217;t. Specifically, the programming puzzle presented on <a href=\"http:\/\/rosettacode.org\/wiki\/McNuggets_problem\" rel=\"noopener noreferrer\" target=\"_blank\">Rosetta Code<\/a> is to identify the highest non-McNuggets number less than 100.<\/p>\n<p>This problem should be attractive to programmers who love to practice their coding kung fu. I thought it might make an interesting Exercise for this blog. But then I looked at the C language solution provided on Rosetta Code, which is shown below. Keep in mind that this isn&#8217;t my code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_11_28-Lesson.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_11_28-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int max = 0, i = 0, sixes, nines, twenties;\r\n \r\nloopstart: while (i &lt; 100)\r\n{\r\n    for (sixes = 0; sixes*6 &lt; i; sixes++)\r\n    {\r\n        if (sixes*6 == i)\r\n        {\r\n            i++;\r\n            goto loopstart;\r\n        }\r\n        for (nines = 0; nines*9 &lt; i; nines++)\r\n        {\r\n            if (sixes*6 + nines*9 == i)\r\n            {\r\n                i++;\r\n                goto loopstart;\r\n            }\r\n            for (twenties = 0; twenties*20 &lt; i; twenties++)\r\n            {\r\n                if (sixes*6 + nines*9 + twenties*20 == i)\r\n                {\r\n                    i++;\r\n                    goto loopstart;\r\n                }\r\n            }\r\n        }\r\n    }\r\n    max = i;\r\n    i++;\r\n}\r\n \r\n    printf(\"Maximum non-McNuggets number is %d\\n\", max);\r\n \r\n    return 0;\r\n}<\/pre>\n<p>Immediately my eyeballs are assaulted by the abundance of heathen <em>goto<\/em> statements in the sacred C code. My first impression upon seeing this C language solution was to wonder whether this is the super rare and perhaps only legitimate example of where <em>goto<\/em> must be used to solve a programming puzzle. I mean, what self-respecting C coder would boldly submit such code for the public to witness? Obviously something is going on here.<\/p>\n<p>The output generates the proper value, so functionally the code is fine:<\/p>\n<p><code>Maximum non-McNuggets number is 43<\/code><\/p>\n<p>The <em>goto<\/em> keyword aside, the code cleverly uses nested loops to plow through values <code>i<\/code> through 100 in various permutations of 6, 9, and 20. This approach is logical, but can it be done without using the despised <em>goto<\/em> statements?<\/p>\n<p>Globally replacing the <em>goto<\/em> statements with a <em>continue<\/em> statement doesn&#8217;t work. I didn&#8217;t think it would, but I had to try. After all, <em>continue<\/em> is another way to keep a loop going while skipping over the rest of the loop&#8217;s statements.<\/p>\n<p>I&#8217;m convinced that the approach shown in this code has merit. But can the code be re-written to avoid using <em>goto<\/em>? If it can&#8217;t, this example provides one legitimate puzzle that requires <em>goto<\/em> in the C language. I just don&#8217;t believe it&#8217;s true!<\/p>\n<p>If you can re-write this code without using <em>goto<\/em>, do it! I have my own solution for the McNuggets problem, which doesn&#8217;t use <em>goto<\/em>, so i know it can be done. I reveal my method in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4479\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Leave to mathematicians to destroy something innocent like the number of nuggets that come in a McDonald&#8217;s Chicken McNuggets box. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4470\">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-4470","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\/4470","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=4470"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4470\/revisions"}],"predecessor-version":[{"id":4504,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4470\/revisions\/4504"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}