Skip to content

Coin change

planning ahead

Level 2 of 3

Also called Change-making problem and Fewest coins, by dynamic programming.

how it works

The idea on coins of 1, 4 and 6 paying 8. Then the whole method on the watch page's first example, coins of 1, 3 and 4 paying 6. There biggest first pays 4, 1 and 1, and the table finds 3 and 3.

You owe 8. You have coins of 1, 4 and 6, as many of each as you like. Take the biggest coin first: 6, then 1 and 1, 3 coins. But 4 and 4 is 2 coins. So that quick rule can be wrong, as the page on greedy choices shows. This method is never wrong. Make a table, with a cell for each amount from 0 to 8. Each cell will hold the fewest coins that pay its amount. Cell 0 holds 0. Then fill the cells from the left. For each amount, try each coin no bigger than it. The coin leaves a smaller amount, whose cell is already filled. That many coins, plus this one, is one way to pay. Keep the fewest. For cell 8, coin 1 leaves 7, which takes 2 coins, so 3. Coin 4 leaves 4, which takes 1, so 2. Coin 6 leaves 2, which takes 2, so 3. Keep 2. If no coin fits, or every coin tried leaves an amount no coins pay, write ∞, read infinity. On coins of 4 and 6, cell 5 is ∞: coin 4 leaves 1, which no coin pays, and coin 6 is too big. The answer is the last cell. To see which coins, read back: from the last cell, pick a coin whose cell it leaves holds 1 fewer, and repeat down to cell 0. Solving each smaller problem once like this is what books call dynamic programming. This page counts tries, one for each coin tried at each amount. The code below makes the same tries, and writes None where the watch page writes ∞.

cell 8 tries each coin641001122331425162728coin 6: cell 2 holds 2, so 3coin 4: cell 4 holds 1, so 2, the fewestcoin 1: cell 7 holds 2, so 3
Each cell holds the fewest coins for the amount under it. Cell 8 reads the cells its 3 coins leave, 7, 4 and 2, each coin beside its arrow.
cellcoin 1coin 4coin 6keep
11--1
22--2
33--3
441-1
522-2
63311
72422
83232
Each try gives the cell its coin leaves, plus 1. A dash is a coin too big to try. 16 tries in all.
reading back from cell 844001122331425162728at 8, coin 4 leaves 4, which holds 1at 4, coin 4 leaves 0, which holds 0paid: 4 and 4, 2 coins
Reading back from cell 8. Each coin lands on a cell holding 1 fewer, until cell 0. Paid 4 and 4.
Biggest first is not fewest: 7 moments of its 21 steps, the steps between them left for the watch page
  1. step 1 of 21

    01234560

    Make 6 from coins of 1, 3 and 4, as few as possible. 0 coins make 0.

    what it remembers to know where it is
    amount
    not yet
    fewest so far
    not yet

    Where it says not yet, nothing is held there at that moment.

    watch from here

  2. step 7 of 21

    0123456012

    For 3, try a coin of 3. That leaves 0, so 3 would take one coin more than 0 does: one coin.

    what it remembers to know where it is
    amount
    3
    fewest so far
    1

    watch from here

  3. step 12 of 21

    012345601211

    The fewest for 4 is one coin, with a coin of 4. Write 1 at 4.

    what it remembers to know where it is
    amount
    4
    fewest so far
    1

    watch from here

  4. step 15 of 21

    012345601211

    For 5, try a coin of 4. That leaves 1, so 5 would take one coin more than 1 does: 2 coins.

    what it remembers to know where it is
    amount
    5
    fewest so far
    2

    watch from here

  5. step 18 of 21

    0123456012112

    For 6, try a coin of 3. That leaves 3, so 6 would take one coin more than 3 does: 2 coins.

    what it remembers to know where it is
    amount
    6
    fewest so far
    2

    watch from here

  6. step 20 of 21

    01234560121122

    The fewest for 6 is 2 coins, with a coin of 3. Write 2 at 6.

    what it remembers to know where it is
    amount
    6
    fewest so far
    2

    watch from here

  7. step 21 of 21

    01234560121122

    Done: 6 takes 2 coins at the fewest.

    what it remembers to know where it is
    amount
    not yet
    fewest so far
    2

    Where it says not yet, nothing is held there at that moment.

    watch from here

the steps, in plain words
  1. 0 coins make the amount 0Python line 7
  2. for each amount from 1 up, try each coin no bigger than itPython lines 8, 9, 10 and 11
  3. the amount that coin leaves, plus the coin: one more than that amount's answerPython lines 12, 13 and 14
  4. keep the fewest, and write it at the amount, or ∞ if no coin leaves an answerPython lines 15 and 16
  5. the answer at the amount asked for is the fewest coinsPython line 17
the same in Python

The steps in plain words are what to learn. Python is a programming language, and the code below does the same steps, for those who code. Python numbers the places in a list from 0, not 1: the first number is numbers[0], the second numbers[1], and so on.

def coin_change(coins, amount):
    """The fewest coins that make amount exactly, any coin used as often as needed.

    coins are different whole numbers from 1 up, and amount is a whole number from 0.
    Returns the fewest coins, or None when no mix of the coins makes amount.
    """
    fewest = [0] + [None] * amount  # cell a holds the fewest coins for a
    for a in range(1, amount + 1):  # each amount from 1 up, in turn
        for coin in coins:
            if coin > a:  # too big for this amount, so not tried
                continue
            left = fewest[a - coin]  # one try: the cell for what it leaves
            if left is None:  # no coins make what it leaves
                continue
            if fewest[a] is None or left + 1 < fewest[a]:
                fewest[a] = left + 1  # one coin more, the fewest so far
    return fewest[amount]  # None if no mix of the coins makes it

how it scales

The plot counts the tries for each amount from 10 to 100,000, on the three kinds of coins section 3 names.

Each mark across or up is ten times the one before. The worst and the average climb one mark up for each mark across. On 1, 2 and 3 that is 297 tries at 100 and 2,997 at 1,000, about 10 times. Here the cost is the tries, and a slope of 1 means they grow as AA does, as section 4's question 1 derives. The best stays flat at 6 tries, however big the amount.

coins tried, ten times more at each mark up

slope 1102104106108101102103104105106107slope 1101102103104105106107108101102103104105106107slope 1101102103104105106107108101102103104105106107

AA, the amount to make, ten times more at each mark to the right

  • coins of 1, 2 and 3, the three smallestsolid where measured by running it; dashed where its formula carries it on to ten millionslope 1: ten times A, 10 times the work
  • three different coins picked at random up to the amount, every set as likelysolid where measured by running it; dashed where its formula carries it on to ten million
  • coins of the amount and the two just below it, the three biggest that fitsolid where measured by running it; dashed where its formula carries it on to ten million
how long the tries for the amount take, at a billion a second
AAcoins triedtime
1,0002,997about 3 microseconds
100,000299,997about 300 microseconds
10,000,00029,999,997about 30 milliseconds

The coins tried are the same on any computer. The times are not: a real computer may be faster or slower than a billion a second.

make these counts yourself

Copy this script and run it with Python 3.12 or later. It counts the same steps as the plot. Where a count depends on a random shuffle, yours will differ a little.

"""Count coin change's tries yourself, as the plot on this page does.

Run it with Python 3.12 or later. For each amount A from 10 to 100,000 it
prints the tries on coins of 1, 2 and 3, on A - 2, A - 1 and A, and the mean
over the plot's own 20 sets of three different coins. The counts are the same
on any computer; only the time differs.
"""

import math


def coin_change(coins, amount):
    """The fewest coins that make amount exactly, any coin used as often as needed.

    coins are different whole numbers from 1 up, and amount is a whole number from 0.
    Returns the fewest coins, or None when no mix of the coins makes amount.
    """
    fewest = [0] + [None] * amount  # cell a holds the fewest coins for a
    for a in range(1, amount + 1):  # each amount from 1 up, in turn
        for coin in coins:
            if coin > a:  # too big for this amount, so not tried
                continue
            left = fewest[a - coin]  # one try: the cell for what it leaves
            if left is None:  # no coins make what it leaves
                continue
            if fewest[a] is None or left + 1 < fewest[a]:
                fewest[a] = left + 1  # one coin more, the fewest so far
    return fewest[amount]  # None if no mix of the coins makes it


def count(coins, amount):
    """How many tries the method makes: at each amount from 1 up, one for
    each coin no bigger than it."""
    tries = 0
    for a in range(1, amount + 1):
        for coin in coins:
            if coin <= a:
                tries += 1
    return tries


def cheapest(amount):
    """The coins that make the fewest tries: the three biggest no bigger than
    the amount, or every coin up to it below 3."""
    if amount < 3:
        return list(range(1, amount + 1)), amount
    return [amount - 2, amount - 1, amount], amount


SPREAD = (math.sqrt(5) - 1) / 2  # the golden ratio's fractional part, 0.618...


def plot_sets(amount):
    """The plot's 20 sets for the average, from 3 up: three coins a third of
    the amount apart, the first at the seed times 0.618..., its whole part
    dropped, so every coin up to the amount is met about as often."""
    sets = []
    for seed in range(1, 21):
        start = (seed * SPREAD) % 1
        coins = [int(((start + j / 3) % 1) * amount) + 1 for j in range(3)]
        for j in (1, 2):
            while coins.index(coins[j]) != j:  # a rounding met another coin
                coins[j] = coins[j] % amount + 1
        sets.append(coins)
    return sets


if __name__ == "__main__":
    print(f"{'A':>7} {'1, 2, 3':>8} {'biggest':>8} {'average':>9}")
    for amount in (10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10_000, 20_000, 50_000, 100_000):
        sets = plot_sets(amount)
        mean = sum(count(coins, amount) for coins in sets) / len(sets)
        print(f"{amount:>7} {count([1, 2, 3], amount):>8} {count(*cheapest(amount)):>8} {mean:>9}")

best, worst and average

Here the size is the amount, AA: the table has a cell for each amount from 0 to AA. For one amount, the coins decide the tries. The watch page's input box takes up to three coins, so this page takes three different coins, each no bigger than the amount, as a bigger coin is never tried. A coin is tried once at each amount from its own value up, so small coins cost the most tries and big ones the fewest. Every case still fills every cell; only the tries differ. In the grid, from 2 up or from 3 up means for every amount of that or more. Below 3, fewer than three different coins fit, so each case takes every coin up to the amount.

A case is one kind of input: here, which coins. A bound says how fast its cost grows as the amount grows.

The counts are in the grid. A count is often made of pieces that grow at different speeds, and as the amount grows, the fastest-growing piece swamps the rest. For example, the tries on 1, 2 and 3, 3A−33A - 3: at 1,000 it is 3,000 less 3, and the 3 hardly matters. So how a cost grows is what tells one method from another, and the bounds keep just that.

what each case costs, and how fast that cost grows; section 4 works out every formula in it
bestcoins of the amount and the two just below it, the three biggest that fitaveragethree different coins picked at random up to the amount, every set as likelyworstcoins of 1, 2 and 3, the three smallest
coins tried6, from 3 up3(A+1)2\frac{3(A + 1)}{2}, from 3 up3A−33A - 3, from 2 up
how the coins tried grow in each case
tight, Θ\ThetaΘ(1)\Theta(1)Θ(A)\Theta(A)Θ(A)\Theta(A)
ceiling, OOO(1)O(1)O(A)O(A)O(A)O(A)
floor, Ω\OmegaΩ(1)\Omega(1)Ω(A)\Omega(A)Ω(A)\Omega(A)

extra memory: A table of one cell for each amount from 0 to the amount owed.

Extra memory is the room it needs on top of the input itself, while it runs.

cointried attries
1amounts 1 to 66
2amounts 2 to 65
3amounts 3 to 64
Coins of 1, 2 and 3 paying 6. Each coin is tried once at every amount from its own value up, 15 tries in all.
amount1, 2 and 3three biggestaveragesets
36661
4967.54
5126910
615610.520
71861235
821613.556
92461584
Tries counted by the run. The average is over every set of three different coins up to the amount, and the last column counts those sets. Each average is halfway between the two columns before it.
digitsbiggest amountcells
1910
299100
39991,000
79,999,99910,000,000
The biggest amount of each length, and the cells its table holds. Each digit more makes 10 times the cells.

where the formula comes from

Five questions, each worked line by line. Question 1 is the count the plot draws.

Question 1: how many tries, TT, on coins of 1, 2 and 3, from 2 up?
  1. the move: count one coin

    the line:
    coin c is triedA−c+1 times\begin{gathered} \text{coin } c \text{ is tried} \\ A - c + 1 \text{ times} \end{gathered}
    amountcoins triedtries
    111
    21, 22
    31, 2, 33
    41, 2, 33
    Coins of 1, 2 and 3 paying 4, 9 tries.

    why it is allowed: Coin cc is tried at each amount from cc up to AA, both ends counted, and at none below its value. For 4, coin 2 is tried at 2, 3 and 4: 4 less 2, plus 1, which is 3 times.

  2. the move: add the three coins

    the line:
    T=A+(A−1)+(A−2)T = A + (A - 1) + (A - 2)

    why it is allowed: By line 1, coin 1 is tried AA times, coin 2 A−1A - 1 times and coin 3 A−2A - 2 times. At 2, coin 3 is never tried, and A−2A - 2 is 0. For 4, 4 + 3 + 2, which is 9.

  3. the move: add them up

    the line:
    T=3A−3T = 3A - 3

    why it is allowed: Three lots of AA, less 1 and less 2. For 4, 12 less 3, which is 9.

  4. the move: a ceiling

    the line:
    T≤3AT \le 3A

    why it is allowed: Taking 3 away leaves less. For 4, 9 is at most 12.

  5. the move: read the bound

    the line:
    T∈O(A)T \in O(A)

    why it is allowed: By line 4, with the fixed multiple 3.

  6. the move: a floor

    the line:
    T≥AT \ge A

    why it is allowed: Take AA from 3A−33A - 3, and 2 lots of AA, less 3, are left. From 2 up, that is at least 1, so TT is more than AA. For 4, 9 is at least 4.

  7. the move: read the bound

    the line:
    T∈Ω(A)T \in \Omega(A)

    why it is allowed: By line 6, with the fixed multiple 1.

  8. the move: both together

    the line:
    T∈Θ(A)T \in \Theta(A)

    why it is allowed: Lines 5 and 7 bound TT by the same growth, only a fixed multiple apart.

try it with a small AA: at A=4A = 4, line 2 gives T=9T = 9, which you can work out by hand

Question 2: how many tries, BB, on the three biggest coins that fit, from 3 up?
  1. the move: count each coin

    the line:
    B=3+2+1B = 3 + 2 + 1
    amountcoins triedtries
    221
    32, 32
    42, 3, 43
    Coins of 2, 3 and 4 paying 4, 6 tries. Amount 1 fits no coin.

    why it is allowed: The three biggest are A−2A - 2, A−1A - 1 and AA. By question 1's line 1, they are tried 3, 2 and 1 times. For 4, coins 2, 3 and 4.

  2. the move: add them up

    the line:
    B=6B = 6

    why it is allowed: 3 + 2 + 1 is 6, with no AA in it, so the same for 4 as for 4 million.

  3. the move: a ceiling

    the line:
    B≤6B \le 6

    why it is allowed: By line 2.

  4. the move: read the bound

    the line:
    B∈O(1)B \in O(1)

    why it is allowed: By line 3, a fixed number of tries, whatever the amount.

  5. the move: a floor

    the line:
    B≥6B \ge 6

    why it is allowed: By line 2 again.

  6. the move: read the bound

    the line:
    B∈Ω(1)B \in \Omega(1)

    why it is allowed: By line 5.

  7. the move: both together

    the line:
    B∈Θ(1)B \in \Theta(1)

    why it is allowed: Lines 4 and 6 bound BB by the same: a fixed 6 tries, no growth.

try it with a small AA: at A=4A = 4, line 1 gives B=6B = 6, which you can work out by hand

Question 3: why does every set of three different coins, each at most AA, make at least 6 tries and at most 3A−33A - 3, from 3 up? Let SS be the fewest any set makes.
  1. the move: line them up

    the line:
    three coins, smallest to biggest\text{three coins, smallest to biggest}

    why it is allowed: The coins are different whole numbers, so each is at least 1 more than the one before. For 4, the sets are 1, 2, 3 and 1, 2, 4 and 1, 3, 4 and 2, 3, 4.

  2. the move: how high each can be

    the line:
    the middle is at most A−1,the smallest at most A−2\begin{gathered} \text{the middle is at most } A - 1, \\ \text{the smallest at most } A - 2 \end{gathered}

    why it is allowed: The biggest is at most AA, and each coin is at least 1 below the next. For 4, the middle is at most 3 and the smallest at most 2.

  3. the move: so each is tried

    the line:
    at least 1,2 and 3times, from the biggest\begin{gathered} \text{at least } 1, 2 \text{ and } 3 \\ \text{times, from the biggest} \end{gathered}
    coincan betried
    smallest1 to 46 down to 3
    middle2 to 55 down to 2
    biggest3 to 64 down to 1
    Three different coins up to 6. Added up, the tries run from 3 + 2 + 1, which is 6, to 6 + 5 + 4, which is 15.

    why it is allowed: By line 2 and question 1's line 1: the bigger a coin, the fewer times it is tried. Coin AA is tried once, coin A−1A - 1 twice and coin A−2A - 2 3 times.

  4. the move: the fewest

    the line:
    S=3+2+1S = 3 + 2 + 1

    why it is allowed: By line 3, no set makes fewer, and question 2's coins make exactly that. For 4, coins 2, 3 and 4 make 6.

  5. the move: how low each can be

    the line:
    the smallest is at least 1,the middle 2, the biggest 3\begin{gathered} \text{the smallest is at least 1,} \\ \text{the middle 2, the biggest 3} \end{gathered}

    why it is allowed: 1 is the smallest coin there is, and each coin is at least 1 more than the one before.

  6. the move: how often at most

    the line:
    they are tried at most A,A−1 and A−2 times\begin{gathered} \text{they are tried at most } A, \\ A - 1 \text{ and } A - 2 \text{ times} \end{gathered}

    why it is allowed: By line 5 and question 1's line 1: the smaller a coin, the more times it is tried.

  7. the move: so the most

    the line:
    at most 3A−3tries\begin{gathered} \text{at most } 3A - 3 \\ \text{tries} \end{gathered}

    why it is allowed: Line 6's tries added up are question 1's line 2, which is 3A−33A - 3, and question 1's coins make exactly that. For 4, every set makes from 6 to 9.

try it with a small AA: at A=4A = 4, line 4 gives S=6S = 6, which you can work out by hand

Question 4: on average, how many tries, MM, do three different coins picked at random make, from 3 up? Each is at most AA, and every set is as likely.
  1. the move: list the sets

    the line:
    the average: every set’s triesadded up, over the sets\begin{gathered} \text{the average: every set's tries} \\ \text{added up, over the sets} \end{gathered}
    setcoin 1coin 2coin 3coin 4tries
    1, 2, 3432-9
    1, 2, 443-18
    1, 3, 44-217
    2, 3, 4-3216
    added up1296330
    The 4 sets up to 4, each coin's tries in its own column by question 1's line 1. Each column holds its coin's tries 3 times, once for each set that holds it.

    why it is allowed: That is what an average is when every set is as likely. For 4, there are 4 sets, and their tries add up to 30, so 7.5.

  2. the move: each coin's share

    the line:
    each coin is in 3 ofevery A sets\begin{gathered} \text{each coin is in 3 of} \\ \text{every } A \text{ sets} \end{gathered}

    why it is allowed: AA is how many coins there are to pick from. Every set holds 3 of them, so the sets hold 3 times as many coins as there are sets. No coin is favoured, so each is in an equal share of the sets, 3 in every AA. For 4, each coin is in 3 of the 4 sets. At 5, the 10 sets hold 30 coins, 6 of each, so each coin is in 6 of the 10.

  3. the move: add by columns

    the line:
    all the tries are each coin’s tries,once for each set holding it\begin{gathered} \text{all the tries are each coin's tries,} \\ \text{once for each set holding it} \end{gathered}

    why it is allowed: Add line 1's grid down its columns instead of along its rows. For 4, 12 + 9 + 6 + 3, which is 30, as before.

  4. the move: divide by the sets

    the line:
    M=3×A+(A−1)+⋯+1AM = 3 \times \frac{A + (A - 1) + \dots + 1}{A}

    why it is allowed: Divide by the number of sets. By lines 2 and 3, each coin's tries count 3 times for every AA sets, so 3 over AA times per set. By question 1's line 1, coins 1 to AA are tried AA, A−1A - 1, and so on down to 1 times. For 4, 4 + 3 + 2 + 1 is 10, and 3 times 10 over 4 is 7.5. At 5, 3 times 15 over 5, which is 9.

  5. the move: pair them up

    the line:
    M=3×A+12M = 3 \times \frac{A + 1}{2}

    why it is allowed: Pair the first number with the last, AA with 1, then A−1A - 1 with 2. Each pair adds to A+1A + 1, so the numbers average half of that. An odd count's middle number is that half too: at 5, 5 + 1 and 4 + 2, and the middle 3 alone is half of 6. For 4, 4 + 1 and 3 + 2, so 10 over 4 is 2.5.

  6. the move: tidy up

    the line:
    M=3(A+1)2M = \frac{3(A + 1)}{2}

    why it is allowed: The same, written as one fraction. For 4, 15 over 2, which is 7.5.

  7. the move: a ceiling

    the line:
    M≤3AM \le 3A

    why it is allowed: From 1 up, A+1A + 1 is at most 2A2A, and half of 3 times 2A2A is 3A3A. For 4, 7.5 is at most 12.

  8. the move: read the bound

    the line:
    M∈O(A)M \in O(A)

    why it is allowed: By line 7, with the fixed multiple 3.

  9. the move: a floor

    the line:
    M≥AM \ge A

    why it is allowed: By line 6, MM is more than half of 3A3A, which is more than AA. For 4, 7.5 is at least 4.

  10. the move: read the bound

    the line:
    M∈Ω(A)M \in \Omega(A)

    why it is allowed: By line 9, with the fixed multiple 1.

  11. the move: both together

    the line:
    M∈Θ(A)M \in \Theta(A)

    why it is allowed: Lines 8 and 10 bound MM by the same growth.

try it on small inputs: over the 4 sets of three different coins up to 4, the average is M=7.5M = 7.5

Question 5: why is every cell right, ∞ included, and how many cells is that? Let KK be that number.
  1. the move: it starts right

    the line:
    cell 0 holds 0\text{cell 0 holds 0}

    why it is allowed: 0 coins pay 0, and nothing pays it with fewer.

  2. the move: suppose so far

    the line:
    suppose every cell leftof this one is right\begin{gathered} \text{suppose every cell left} \\ \text{of this one is right} \end{gathered}
    cell 8 tries each coin641001122331425162728coin 6: cell 2 holds 2, so 3coin 4: cell 4 holds 1, so 2, the fewestcoin 1: cell 7 holds 2, so 3
    Cell 8 is filled last. Cells 0 to 7 are to its left, and its 3 tries read cells 7, 4 and 2.

    why it is allowed: Cells fill from the left, so this cell reads only cells already filled. Suppose, for now, that each of those is right. Lines 3 to 7 show that this cell is then right too, and line 8 then covers every cell.

  3. the move: a try is a real way

    the line:
    each try with a numberpays this amount\begin{gathered} \text{each try with a number} \\ \text{pays this amount} \end{gathered}

    why it is allowed: A coin no bigger than the amount leaves a smaller amount. Pay that in the fewest coins, the number its cell holds by line 2, then add the coin. On 1, 4 and 6, cell 8's coin 4 leaves 4, paid by one 4, so 4 and 4.

  4. the move: so at most

    the line:
    the fewest is at mostthe smallest try\begin{gathered} \text{the fewest is at most} \\ \text{the smallest try} \end{gathered}

    why it is allowed: Each try with a number is a way to pay, by line 3, so the fewest can be no more than any of them. For 8, the tries give 3, 2 and 3.

  5. the move: the best way is a try

    the line:
    some try is at mostthe fewest\begin{gathered} \text{some try is at most} \\ \text{the fewest} \end{gathered}

    why it is allowed: If some way pays this amount, take a best one, and any coin in it. That coin is no bigger than the amount, so it is tried. The other coins pay what it leaves, so there are at least as many as that cell holds, by line 2. With the coin, that is at least its try. For 8, a best way is 4 and 4. The other 4 pays 4, at least cell 4's 1 coin, so at least 2, coin 4's try.

  6. the move: so the cell is right

    the line:
    this cell holdsthe fewest\begin{gathered} \text{this cell holds} \\ \text{the fewest} \end{gathered}

    why it is allowed: By lines 4 and 5, the smallest try is exactly the fewest, and that is what the cell keeps. For 8, 2.

  7. the move: and ∞

    the line:
    no try with a numbermeans no way at all\begin{gathered} \text{no try with a number} \\ \text{means no way at all} \end{gathered}
    amountcoin 4coin 6holds
    1--∞
    2--∞
    3--∞
    41-1
    5∞-∞
    6∞11
    7∞∞∞
    82∞2
    Coins of 4 and 6 up to 8. A try that leaves ∞ gives ∞, and a dash is a coin too big. A cell holds ∞ when no try gives a number.

    why it is allowed: Any way to pay holds some coin no bigger than the amount. The rest pay what it leaves, so that cell would hold a number, and its try would too. On coins of 4 and 6, 5 is ∞, as coin 4 leaves 1, which is ∞.

  8. the move: every cell

    the line:
    every cell is right\text{every cell is right}

    why it is allowed: By line 1, cell 0 is right. By lines 2 to 7, each later cell is right once those before it are. So, filling from the left, every cell is, the last included.

  9. the move: count them

    the line:
    K=A+1K = A + 1

    why it is allowed: One cell for each amount from 0 to AA, each right by line 8. For 4, 5 cells.

try it with a small AA: at A=4A = 4, line 9 gives K=5K = 5, which you can work out by hand

count it yourself

Coins of 1, 2 and 3 paying 4, step by step. Each try adds 1 to the count on its own step.

1, 2, 3; 4, every step written down
stepwhat happenscoins tried
1Make 4 from coins of 1, 2 and 3, as few as possible. 0 coins make 0.0
2For 1, try a coin of 1. That leaves 0, so 1 would take one coin more than 0 does: one coin.1
3The fewest for 1 is one coin, with a coin of 1. Write 1 at 1.1
4For 2, try a coin of 1. That leaves 1, so 2 would take one coin more than 1 does: 2 coins.2
5For 2, try a coin of 2. That leaves 0, so 2 would take one coin more than 0 does: one coin.3
6The fewest for 2 is one coin, with a coin of 2. Write 1 at 2.3
7For 3, try a coin of 1. That leaves 2, so 3 would take one coin more than 2 does: 2 coins.4
8For 3, try a coin of 2. That leaves 1, so 3 would take one coin more than 1 does: 2 coins.5
9For 3, try a coin of 3. That leaves 0, so 3 would take one coin more than 0 does: one coin.6
10The fewest for 3 is one coin, with a coin of 3. Write 1 at 3.6
11For 4, try a coin of 1. That leaves 3, so 4 would take one coin more than 3 does: 2 coins.7
12For 4, try a coin of 2. That leaves 2, so 4 would take one coin more than 2 does: 2 coins.8
13For 4, try a coin of 3. That leaves 1, so 4 would take one coin more than 1 does: 2 coins.9
14The fewest for 4 is 2 coins, with a coin of 1. Write 2 at 4.9
15Done: 4 takes 2 coins at the fewest.9

In all, 9 coins tried. The formula from section 4, 3A−33A - 3, gives T=9T = 9 at A=4A = 4: the same.

where you meet it

The table checks a set of coins, and helps choose one.

  • Is biggest first safe for these coins?

    Dexter Kozen and Shmuel Zaks showed this in a paper published in 1994. For a set of coins with a coin of 1, if biggest coin first is ever wrong, it is already wrong below the two biggest coins added up. On 1, 4 and 6, those add up to 10, and biggest first is first wrong at 8. So one way to check a set is this table up to there, set beside biggest first.

    source: D. Kozen and S. Zaks, Optimal bounds for the change-making problem (opens in a new tab), Theoretical Computer Science 123, 1994

  • Australian coins

    On coins of 5, 10, 20 and 50 cents, 1 dollar and 2 dollars, biggest coin first is right for every amount they can pay. The books checked it on every multiple of 5 cents up to 20 dollars, 400 amounts.

    source: the books' volume three, chapter 30

  • An 18 cent coin

    Jeffrey Shallit asked which four coins make change from 0 to 99 cents in the fewest coins on average. On 1, 5, 10 and 25 cents it is 4.7. On 1, 5, 18 and 25 cents, or 1, 5, 18 and 29, it is 3.89. On 1, 5, 18 and 25, biggest first pays 36 cents as 25, 5, 5 and 1, where 18 and 18 is 2 coins. So the fewest needs a table like this one.

    source: J. Shallit, What this country needs is an 18¢ piece (opens in a new tab), The Mathematical Intelligencer 25, 2003, and his preprint (opens in a new tab)

what is used instead

On coins where biggest first is safe, such as Australia's, it is quicker: a few steps, no table.

how it compares

Biggest coin first is quick, and wrong on some coins. The plain method works out an amount by asking for the fewest for each amount its coins leave. Each ask is a call, and the same amounts are worked out again and again. A memo stores each amount's answer, so a repeat asks for nothing more, as the page on solving each smaller problem once shows. The table fills every cell from the left and makes no calls.

14 calls, 7 different problems65432100102100
  • first time
  • asked before
The plain method on 6, with coins of 1, 4 and 6. Each circle is a call, holding its amount, joined to the call that asked for it. 14 calls, for 7 different amounts.
11 calls: 7 worked out, 4 answered at once65432100120
  • first time
  • asked before, answered at once
The same with a memo. A repeat asks for nothing more, so 14 calls become 11.
waycoins for 20work for 20
biggest coin first55 coins taken
plain method43,053 calls
with a memo453 calls
the table452 tries
Coins of 1, 4 and 6 paying 20. Biggest first pays 6, 6, 6, 1 and 1, where 6, 6, 4 and 4 is 4 coins. A call and a try are different steps, so read this for the size of each number.

race them on one row of your own