Skip to content

The McNuggets Challenge

Anders Peterson edited this page May 9, 2019 · 4 revisions

There is an updated version of this available at https://www.ojalgo.org/2019/05/the-mcnuggets-challenge/


Chicken McNuggets can be ordered in the sizes of 6, 9 and 20 pieces. Being allowed to use a combination of the sizes, what is the highest number that you can NOT order?

Example: 98 pieces can be ordered using a combination (4x20 pieces + 2x9 pieces). 14 pieces cannot be ordered (no combination of 6, 9 and 20 equals 14)

Code that solves the problem

import org.ojalgo.OjAlgoUtils;
import org.ojalgo.netio.BasicLogger;
import org.ojalgo.optimisation.Expression;
import org.ojalgo.optimisation.ExpressionsBasedModel;
import org.ojalgo.optimisation.Variable;
import org.ojalgo.optimisation.Optimisation.Result;

public class TheMcNuggetsChallenge {

    public static void main(String[] args) {
        
        BasicLogger.debug();
        BasicLogger.debug(TheMcNuggetsChallenge.class.getSimpleName());
        BasicLogger.debug(OjAlgoUtils.getTitle());
        BasicLogger.debug(OjAlgoUtils.getDate());
        BasicLogger.debug();

        final ExpressionsBasedModel model = new ExpressionsBasedModel();

        Variable numberOfPack6 = model.addVariable("#6-packs").lower(0).integer(true);
        Variable numberOfPack9 = model.addVariable("#9-packs").lower(0).integer(true);
        Variable numberOfPack20 = model.addVariable("#20-packs").lower(0).integer(true);

        Expression totalNuggetsOrdered = model.addExpression().weight(1);
        totalNuggetsOrdered.set(numberOfPack6, 6);
        totalNuggetsOrdered.set(numberOfPack9, 9);
        totalNuggetsOrdered.set(numberOfPack20, 20);

        for (int i = 100; i > 0; i--) {
            totalNuggetsOrdered.upper(i);
            Result result = model.maximise();

            if (Math.round(result.getValue()) < i) {
                BasicLogger.debug("Not possible to order {} nuggets", i);
                break;
            } else {
                BasicLogger.debug(result);
            }
        }
    }

}

If you believe there is a number greater than 100 that cannot be ordered, then you need to modify and run the code yourself.

Console output

TheMcNuggetsChallenge
ojAlgo
2018-04-01

OPTIMAL 100.0 @ [0.0, 0.0, 5.0]
OPTIMAL 99.0 @ [2.0, 3.0, 3.0]
OPTIMAL 98.0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