Skip to content

make change

Pay an amount with the fewest coins. Race greedy change-making, biggest coin first, against the coin change table, which works out every amount from 0 up.

A try is one coin held against an amount. Greedy counts every coin it holds, even one too big. The table skips a coin bigger than the amount without a try. Fewest tries wins the race. Beside the verdict, the race says how many coins each method paid.

In the race below each method has a lane. At each tick of the clock, every lane still running makes one more try.

Both race on coins of 1, 4 and 6, paying 8. Greedy makes 6 tries and pays 6, 1 and 1: 3 coins. The table makes 16 tries and pays 4 and 4: 2 coins. Greedy: fewer tries, but more coins.

Greedy change-making holds the biggest coin against what is still owed. If it fits, it takes it and tries it again. If not, it moves on to the next smaller coin. It stops when even the smallest coin does not fit.

holdagainst what is leftfitscoins so far
68yes, take it1
62no1
42no1
12yes, take it2
11yes, take it3
10no3
3 coins taken, plus one no for each of the 3 kinds of coin, 6, 4 and 1: 6 tries. Greedy pays 6, 1 and 1, where 4 and 4 would be 2 coins.

The table, coin change, has a cell for each amount from 0 to 8. The cells are filled from the left, and cell 0 holds 0. At each amount it tries every coin no bigger than that amount. Taking the coin leaves a smaller amount, as coin 4 at 8 leaves 4, whose cell is filled already. The cell holds 1 more than the smallest of those cells.

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
Cells 0 to 8 hold 0, 1, 2, 3, 1, 2, 1, 2, 2. Cell 8 tries coins 1, 4 and 6: they leave cells 7, 4 and 2, holding 2, 1 and 2. So cell 8 holds 1 more than 1: 2, paid 4 and 4. Coin 1 is tried at amounts 1 to 8, coin 4 at 4 to 8, coin 6 at 6 to 8: 8 + 5 + 3 = 16 tries.

or make one

race these, up to four

one tick is

Without scripts the race cannot run, so each lane shows where its racer ends.

Tick 16: no lane has made more than 16 coins tried. Greedy change-making and coin change have finished.

Every lane pays 8.

greedy change-making

done in 6 coins tried
0×41×42×40×61×68523

coin change

done in 16 coins tried
012345678012312122

what the marks mean

  • a coin held against an amount, a cell being read, or greedy's own way when it pays with more coins
  • a cell being written
  • a cell whose number is final
  • the fewest coins found
  • a way whose coins pay more than the amount
  • any other cell
  • how much of its race a lane has run

which suits these coins

Paying 8, greedy change-making made the fewest coins tried: 6, against 16 for coin change.

What each lane paid: greedy change-making 3 coins and coin change 2 coins.

each racer's own page

The same racers paying more with one set of coins (1, 2 and 5): coins tried up, the amount across, both scales in powers of ten. Each point is counted on that payment.
102104101102103104105106107101102103104101102103104105106107
  • greedy change-making
  • coin change

the starting racers, then paying more

Paying 8 with 1, 4 and 6, greedy makes 6 tries to the table's 16. The table tries each coin at nearly every amount, greedy once for each coin it pays, so a bigger amount widens the gap. Pay 1,000 with coins of 1, 2 and 5. The table tries coin 1 at the 1,000 amounts from 1 up, and coin 2 at the 999 from 2 up. Coin 5 is tried at the 996 from 5 up: 2,995 tries. Greedy takes 200 fives, one try each, plus one no for each of the 3 kinds of coin: 203.

The second row pays 1,000 with coins of 1, 2 and 5. The fewest in each row is in bold.

greedy change-makingcoin change
the starting coins and amount1, 4, 6; 8616
paying 1,0002032,995

the starting racers paying 8 with each set of coins

Each button under the race's box picks a set of coins, and the race's table below gives both methods' tries paying 8 with each. Greedy makes the fewest tries with four of the five. With 1 and 9 the table wins, 8 tries to 10, as the first picture below shows. The 9 is bigger than every amount, so the table skips it and tries only the 1, once at each amount. Greedy holds the 9 against 8, takes eight 1s, then holds a 1 against 0: 10 tries. Fewest tries is not the whole verdict. With 1, 4 and 5, greedy pays four coins where 4 and 4 is two, as the second picture shows. One amount does not show whether greedy always pays the fewest: with 1, 4 and 5 it pays 4 right, then 8 wrong. With 1, 2 and 5 greedy always pays the fewest, as the last picture shows, so use greedy there. Unless you know greedy always pays the fewest with your coins, use the table: it always finds the fewest.

coingreedy holds itthe table tries it
91 time: nonever: too big
19 times: 8 yes, 1 no8 times: amounts 1 to 8
all108
Paying 8 with 1 and 9. Greedy holds every coin it meets, even the 9. The table skips the 9 at every amount, uncounted.
holdagainst what is leftfitscoins so far
58yes, take it1
53no1
43no1
13yes, take it2
12yes, take it3
11yes, take it4
10no4
Greedy paying 8 with 1, 4 and 5. After the 5, 3 is owed: the 5 and the 4 are too big, so three 1s. 7 tries, four coins, where 4 and 4 is two.
never in the fewest coinsswap forcoins saved
1, 121
2, 2, 25, 11
2, 2, 152
With 1, 2 and 5, the fewest coins never hold these: each swap pays the same with fewer. So at most one 1 and two 2s, never both: they pay 4 at most, less than a 5. So the fewest take every 5 that fits, then pay the rest under 5 with the fewest 2s and 1s, as greedy does.

Their coins tried on the payment the race makes with each set of coins.

greedy change-makingcoin change
1, 2 and 51, 2, 5; 8619
1, 4 and 51, 4, 5; 8717
1, 2 and 31, 2, 3; 8621
1 and 91, 9; 8108
1 and two coins at random1, 6, 7; 8513