Skip to content

Coin change

planning ahead

Level 2 of 3

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

test yourself

Six questions, four to pick from and two for paper.

fresh numbers: the questions a run answers, asked again on new numbers

choose one

question 1

How many tries does the method make on coins of 1, 2 and 3 paying 7?

the answer, and why

the answer is D

Coin 1 is tried at 7 amounts, coin 2 at 6 and coin 3 at 5, which is 18.

if you chose another

  • That tries every coin at every amount, but coins 2 and 3 are too big for the first amounts.
  • That is the number of amounts filled, not the tries.
  • That is the fewest coins for 7, as 3, 3 and 1.

question 2

Coins of 1, 4 and 6 pay 9. What is the fewest number of coins?

the answer, and why

the answer is D

4, 4 and 1. Cell 9 tries coin 1, leaving 8, which takes 2, so 3. No 2 coins add to 9.

if you chose another

  • That is biggest coin first, 6, 1, 1 and 1.
  • No two of these coins add to 9.
  • That pays in coins of 1 only.

question 3

Each set pays 9. Which makes the fewest tries?

the answer, and why

the answer is C

The biggest coins are tried the fewest times, 3 + 2 + 1, which is 6.

this tests best, worst and average, on the learn page

if you chose another

  • The smallest coins make the most, 9 + 8 + 7, which is 24.
  • 8 + 5 + 1, which is 14.
  • 9 + 2 + 1, which is 12.

question 4

The table for 8 is filled. Which amounts can you now answer with no more tries?

the answer, and why

the answer is C

Each cell holds the fewest for its own amount, so the whole row answers every amount up to 8 at once, as the books note (volume three, chapter 38).

this tests how it works, on the learn page

if you chose another

  • Every cell on the way is a full answer too.
  • A cell no coins pay holds ∞, which is an answer as well.
  • The cells are already filled.

pen and paper

question 5about 6 minutes

Run the method by hand on coins of 2, 3 and 7 paying 9.

  1. Fill cells 0 to 9.
  2. How many tries are there?
  3. Read back the coins for 9.
a hint

Cell 1 fits no coin.

a worked answer

Cells 0 to 9 hold 0, ∞, 1, 1, 2, 2, 2, 1, 3, 2. Cell 1 is ∞, as no coin fits. At 8, coin 7 leaves 1, which is ∞, so it is no help. Coin 2 is tried at 8 amounts, coin 3 at 7 and coin 7 at 3, which is 18 tries. Cell 9 holds 2. Try the coins from the biggest: coin 7 leaves 2, which holds 1, one fewer, so 7 is a right coin. Then coin 2 leaves 0. Paid 7 and 2.

this tests how it works, on the learn page

what a full answer includes
  • 0, ∞, 1, 1, 2, 2, 2, 1, 3, 2.
  • 18 tries.
  • 7 and 2.

question 6about 6 minutes

A friend says the table is quick, since its tries grow only as the amount does.

  1. How many tries for 1,000,000,000 on coins of 1, 2 and 3?
  2. How long is that at a billion tries a second?
  3. How many cells does the table hold, and what does that say?
a hint

Question 1 of the learn page's section 4 counts the tries.

a worked answer

3 times 1,000,000,000, less 3, which is 2,999,999,997 tries, about 3 seconds. The table holds 1,000,000,001 cells, one for each amount from 0. The amount is only 10 digits long, yet the work grows with its size. Each digit more makes it about 10 times bigger.

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

this tests where the formula comes from, on the learn page

what a full answer includes
  • 2,999,999,997 tries.
  • About 3 seconds.
  • 1,000,000,001 cells: this amount fills over a billion cells.