The Chicken McNuggets Problem

Box of 20 Chicken McNuggets, McDonalds
Chicken 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.

Yes, 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.

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’t a combination of 6, 9, and/or 20 is a non-McNuggets number.

Using the Power Of The Computer, it’s possible to determine which values are McNuggets numbers and which aren’t. Specifically, the programming puzzle presented on Rosetta Code is to identify the highest non-McNuggets number less than 100.

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’t my code:

2020_11_28-Lesson.c

#include <stdio.h>

int main()
{
    int max = 0, i = 0, sixes, nines, twenties;
 
loopstart: while (i < 100)
{
    for (sixes = 0; sixes*6 < i; sixes++)
    {
        if (sixes*6 == i)
        {
            i++;
            goto loopstart;
        }
        for (nines = 0; nines*9 < i; nines++)
        {
            if (sixes*6 + nines*9 == i)
            {
                i++;
                goto loopstart;
            }
            for (twenties = 0; twenties*20 < i; twenties++)
            {
                if (sixes*6 + nines*9 + twenties*20 == i)
                {
                    i++;
                    goto loopstart;
                }
            }
        }
    }
    max = i;
    i++;
}
 
    printf("Maximum non-McNuggets number is %d\n", max);
 
    return 0;
}

Immediately my eyeballs are assaulted by the abundance of heathen goto 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 goto 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.

The output generates the proper value, so functionally the code is fine:

Maximum non-McNuggets number is 43

The goto keyword aside, the code cleverly uses nested loops to plow through values i through 100 in various permutations of 6, 9, and 20. This approach is logical, but can it be done without using the despised goto statements?

Globally replacing the goto statements with a continue statement doesn’t work. I didn’t think it would, but I had to try. After all, continue is another way to keep a loop going while skipping over the rest of the loop’s statements.

I’m convinced that the approach shown in this code has merit. But can the code be re-written to avoid using goto? If it can’t, this example provides one legitimate puzzle that requires goto in the C language. I just don’t believe it’s true!

If you can re-write this code without using goto, do it! I have my own solution for the McNuggets problem, which doesn’t use goto, so i know it can be done. I reveal my method in next week’s Lesson.

2 thoughts on “The Chicken McNuggets Problem

  1. goto? Am I allowed to say WTF in your comments?

    I haven’t given this much thought but I suspect it would be better to hard-code 6, 9 and 20 into an array and use a single loop. This would also make it easier to allow for any changes in quantities.

    You could extend this with a problem like “if you have x number of people, what is the lowest combination of boxes necessary to give them at least n nuggets each?”

  2. Interesting solution proposal. It sounds like what I did, though we’ll have to wait a week to see if our thoughts are inline.

    I like the extension you proposed. It reminds me of the pizza problem: Given n people, how many ways can you slice a pizza so that everyone gets the same number of pieces?

Leave a Reply