The Frobenius Coin Problem and the Chicken Nugget Question
Find the largest total you cannot make from given coin values or box sizes, with proofs for two sizes and a clean method for three.
Key takeaways
- If the coin values share no common factor, only finitely many amounts are impossible, and the largest is called the Frobenius number.
- For two coprime values a and b the Frobenius number is ab − a − b.
- For two such values exactly half of the amounts from 0 to ab − a − b can be made.
- With three or more values there is no comparably simple formula, but remainder tables compute the answer efficiently.
- For the boxes of 6, 9 and 20 the largest impossible order is 43.
Suppose nuggets come only in boxes of 6, 9 and 20. What is the largest number of nuggets you cannot buy exactly? The answer is 43. Every whole number from 44 upward can be made from some combination of those boxes.
This is a case of the Frobenius coin problem: given coin values with no common factor, find the largest amount that cannot be paid exactly using any number of each. The box sizes are a classic puzzle version, inspired by fast-food packaging. Actual menu sizes vary.
Setting up the question
Call an amount representable if it equals a sum of the given values, each used zero or more times. Zero is representable by using nothing. Negative quantities are not allowed: you cannot hand a box back.
That last rule is what makes the problem interesting. If subtraction were allowed, Bezout's identity would let you reach every multiple of the greatest common divisor. With only addition, small amounts have gaps.
If all values share a factor d greater than 1, then every total is a multiple of d, and there are infinitely many impossible amounts. So the problem assumes the greatest common divisor of the whole set is 1. The values need not be coprime in pairs: 6, 9 and 20 share factors two at a time but not all together.
Why there is a largest impossible amount
Before hunting for a number it is worth knowing that it exists. With the values 6, 9 and 20, some combination using positive and negative counts equals 1, for instance 1 = 6 × 2 + 9 − 20.
The negative count is the problem. Cure it with a cushion of five boxes of 20, which makes 100. Now add k copies of that expression for 1:
100 + k = (5 − k) × 20 + k × 9 + 2k × 6 for k = 0, 1, ..., 5 No count on the right is negative, so every total from 100 to 105 can be made. Once six consecutive totals are available, adding sixes reaches everything above them.
The same argument works for any set of values with greatest common divisor 1. It guarantees that the gaps eventually stop, though the bound it gives is usually far larger than the truth.
Proving 43 for the nuggets
First, 43 is impossible. You could use zero, one or two boxes of 20, leaving 43, 23 or 3. The remainder would have to be made from 6s and 9s, which only produce multiples of 3. That excludes 43 and 23. And 3 is smaller than either box.
Second, every larger amount is possible. Check six in a row:
| amount | boxes |
|---|---|
| 44 | 20 + 6 × 4 |
| 45 | 9 × 5 |
| 46 | 20 × 2 + 6 |
| 47 | 20 + 9 × 3 |
| 48 | 6 × 8 |
| 49 | 20 × 2 + 9 |
Any larger number is one of these plus some boxes of 6. This finishes the proof. The trick of establishing a run as long as the smallest value, then adding that value repeatedly, is a form of strong induction.
Two values: the clean formula
Take values 3 and 5. The impossible amounts are 1, 2, 4 and 7. The largest, 7, equals 3 × 5 − 3 − 5.
Here is why the formula holds for coprime a and b. Sort amounts by their remainder on division by a. The numbers 0, b, 2b, ..., (a − 1)b leave all a different remainders, because b is coprime to a.
For each of these remainders, the number rb is the smallest representable amount in its class. Every amount in the class that is at least rb can be made, by adding copies of a. Every smaller one cannot.
So the largest impossible amount comes from the worst class, r = a − 1:
(a − 1)·b − a = ab − a − b For 7 and 11 this gives 77 − 18 = 59. For 4 and 9 it gives 36 − 13 = 23.
A picture in the plane
You can see the two-value case as geometry. Each way of paying with x coins of value 3 and y coins of value 5 is a grid point (x, y) with neither coordinate negative. The amount paid is 3x + 5y, which is constant along slanting parallel lines.
An amount n can be made exactly when its line passes through at least one grid point in that quarter of the plane. For large n the segment of the line inside the quarter is long, longer than the spacing between neighboring grid points on it, so it must contain one. For small n the segment is short and may slip between them. The spacing along the line comes from trading 5 coins of value 3 for 3 coins of value 5.
The symmetry of two values
Let g = ab − a − b. For any whole number n from 0 to g, exactly one of n and g − n is representable. With 3 and 5 the pairs are (0, 7), (1, 6), (2, 5) and (3, 4), and in each pair one member works.
It follows that the number of impossible amounts is half of g + 1, that is, (a − 1)(b − 1)/2. For 3 and 5 this is 4, matching the list. J. J. Sylvester studied these counts in the nineteenth century.
Pairing arguments like this, where each item is matched with a partner of the opposite type, are a favorite device in combinatorial proof.
A worked payment problem
Say a vending machine accepts tokens worth 5 and 8 units. The largest total you cannot pay exactly is 40 − 13 = 27, and there are (4 × 7)/2 = 14 impossible totals.
How would you pay 53? Work modulo 5: since 8 leaves remainder 3, you need 3y ≡ 53 ≡ 3, so y ≡ 1. With y = 1 the remaining 45 is nine tokens of 5. With y = 6 the remaining 5 is a single token. Those are the only two ways, because y = 11 would already exceed 53.
This is solving a linear congruence followed by a size check. The congruence finds the allowed values of y, and the requirement that x not be negative cuts the list off.
A method for three or more values
The remainder idea extends to any list. Pick the smallest value, here 6. For each remainder from 0 to 5, find the least representable amount with that remainder using the other values.
| remainder mod 6 | smallest representable | how |
|---|---|---|
| 0 | 0 | nothing |
| 1 | 49 | 20 + 20 + 9 |
| 2 | 20 | 20 |
| 3 | 9 | 9 |
| 4 | 40 | 20 + 20 |
| 5 | 29 | 20 + 9 |
An amount is representable exactly when it is at least the table entry for its remainder. The largest impossible amount is the largest entry minus 6, which is 49 − 6 = 43.
The table also counts the gaps. Each entry w with remainder r hides (w − r)/6 impossible amounts. Those counts are 0, 8, 3, 1, 6 and 4, for a total of 22.
Finding the table by computer
For larger problems, treat the remainders as locations, with a road from r to r + c (reduced modulo m) of length c for each other value c. The least representable amount in each class is the shortest route from 0. A standard shortest-path method such as Dijkstra's algorithm fills the table quickly, even for many values, as long as the smallest one is moderate.
A simpler program marks 0 as reachable and then marks n as reachable if n − c is reachable for some value c. Once you see m consecutive reachable amounts, where m is the smallest value, you can stop.
How hard is the general problem?
For two values we have a formula. For three values nothing as tidy exists: Frank Curtis showed in 1990 that no finite set of polynomial formulas can give the Frobenius number in all three-value cases, although efficient algorithms exist.
If the number of values is part of the input, computing the Frobenius number is NP-hard, as Jorge Ramírez-Alfonsín showed in the 1990s. Ravi Kannan showed that for any fixed number of values there is a polynomial-time algorithm. These distinctions are explained in hard computational problems.
Numerical semigroups
The set of all representable amounts is closed under addition and contains 0. Mathematicians call such a set, when only finitely many whole numbers are missing from it, a numerical semigroup. The missing numbers are its gaps, and the Frobenius number is the largest gap.
These objects appear in algebraic geometry, in the study of certain curve singularities, and in coding theory. Several simple questions about them remain unanswered. A conjecture of Herbert Wilf, proposed in 1978, relates the number of generators to the proportion of representable amounts below the largest gap, and as of the mid-2020s it was proved only in special cases.
Variations to try
- Allow each box at most a limited number of times. The set of totals becomes finite and symmetric about half the maximum.
- Find the largest sports score that is impossible with only 3-point and 7-point plays. The formula gives 21 − 10 = 11.
- Add a fourth box of 4 to the nuggets and rebuild the table using remainders modulo 4.
- Ask how many different ways an amount can be made, a question answered by generating functions.
Common mistakes
Applying ab − a − b to values with a common factor gives nonsense: for 4 and 6, every odd number is impossible. Applying it to three values also fails. And checking that a few large numbers work does not show that all do. You need a run of consecutive successes at least as long as the smallest value, which then propagates forever.