Lesson
The theory — Coin Change: Greedy against Optimal
The change-making problem asks for the fewest coins, from a given set of denominations with any number of each, that add up to an amount. The greedy algorithm takes the largest coin that fits and repeats. Dynamic programming solves the problem exactly by solving every smaller amount first: M(0) = 0 and M(a) = 1 + min M(a − c) over the coins c ≤ a. A coin system on which greedy is optimal for every amount is called canonical.
What each symbol means
M(a)- the fewest coins that make the amount a; the number in cell a of the table
G(a)- the number of coins greedy uses for a
cₘ, cₘ₋₁- the largest and second-largest coins; the first amount where greedy loses, if there is one, is smaller than their sum
- Assumes
- An unlimited supply of every coin and whole-number denominations, with the number of coins as the only cost. A till that has run out of florins is a different problem. The bound needs a 1 among the coins; without one, some amounts cannot be paid at all, and greedy can stall short of an amount the table can pay.
Problem solved in full
-
Which third coin can join 1 and 5? 3 steps
A country has coins of 1 and 5 and wants a third, d, somewhere from 6 to 30. For which d will greedy change-giving always be optimal? The page tests one system at a time; Kozen and Zaks's rule for three coins settles all 25 at once. Press 1, 3, 4 to try the rule first on a system the page has already tested.
-
The rule, for coins 1 < c < d: write d = qc + r with 0 ≤ r < c. The system fails somewhere if and only if 0 < r < c − q. For 1, 3, 4, d = 4 = 1 × 3 + 1, so q = 1 and r = 1, and 0 < 1 < 2 holds. It fails, and the page finds the first failure at 6:
-
Now c = 5, so d = 5q + r and the test is 0 < r < 5 − q. The larger q is, the less room is left for r:
-
For q = 1 (d from 6 to 9), r can be 1, 2 or 3, so d = 6, 7 and 8 fail. For q = 2 (10 to 14), r = 1 or 2: 11 and 12. For q = 3 (15 to 19), only r = 1: 16. From q = 4 on, 5 − q is 1 or less and no r fits:
Answer
Six values fail, 6, 7, 8, 11, 12 and 16, and the other nineteen work, every d from 17 to 30 among them. Type
1, 5, 12to watch one of the six: greedy pays 15 as 12 + 1 + 1 + 1, four coins, where three fives do it. -
Learning path
Why an algorithm is correct
References (3)
- The bound the page draws as a dashed line and settles every system with: if greedy is ever beaten, the smallest amount where it is beaten lies strictly between c₃ + 1 and cₘ + cₘ₋₁; the three-coin rule the worked problem uses; the {1, 3, 4} example and the US system 1, 5, 10, 25, 50, 100 as canonical: D. Kozen and S. Zaks, "Optimal bounds for the change-making problem." Theoretical Computer Science 123(2), 1994, pp. 377-388. DOI 10.1016/0304-3975(94)90134-1
- The pre-decimal coins and their values in pence (threepence, sixpence, shilling of 12d, florin of two shillings, half-crown of two shillings and sixpence, crown of five shillings), and the halfpenny's end as legal tender on 31 July 1969: Wikipedia contributors, "£sd", Wikipedia, The Free Encyclopedia, accessed 3 October 2026.
- Which coins made up the circulating set at the end, as the Royal Mint itself issued them in 1970: half-crown, florin, shilling, sixpence, threepence, penny and halfpenny: The Royal Mint, "1970 Pre-Decimal UK Proof Set", product description, accessed 3 October 2026.