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.
| hold | against what is left | fits | coins so far |
|---|---|---|---|
| 6 | 8 | yes, take it | 1 |
| 6 | 2 | no | 1 |
| 4 | 2 | no | 1 |
| 1 | 2 | yes, take it | 2 |
| 1 | 1 | yes, take it | 3 |
| 1 | 0 | no | 3 |
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.
or make one
race these, up to four
Choose two to four racers.
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.
No tick the race offers is counted by every one of these racers, so one clock cannot time them. Choose others.
Every lane pays 8.
greedy change-making
done in 6 coins triedcoin change
done in 16 coins triedwhat 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
- 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-making | coin change | |
|---|---|---|
| the starting coins and amount1, 4, 6; 8 | 6 | 16 |
| paying 1,000 | 203 | 2,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.
| coin | greedy holds it | the table tries it |
|---|---|---|
| 9 | 1 time: no | never: too big |
| 1 | 9 times: 8 yes, 1 no | 8 times: amounts 1 to 8 |
| all | 10 | 8 |
| hold | against what is left | fits | coins so far |
|---|---|---|---|
| 5 | 8 | yes, take it | 1 |
| 5 | 3 | no | 1 |
| 4 | 3 | no | 1 |
| 1 | 3 | yes, take it | 2 |
| 1 | 2 | yes, take it | 3 |
| 1 | 1 | yes, take it | 4 |
| 1 | 0 | no | 4 |
| never in the fewest coins | swap for | coins saved |
|---|---|---|
| 1, 1 | 2 | 1 |
| 2, 2, 2 | 5, 1 | 1 |
| 2, 2, 1 | 5 | 2 |
Their coins tried on the payment the race makes with each set of coins.
| greedy change-making | coin change | |
|---|---|---|
| 1, 2 and 51, 2, 5; 8 | 6 | 19 |
| 1, 4 and 51, 4, 5; 8 | 7 | 17 |
| 1, 2 and 31, 2, 3; 8 | 6 | 21 |
| 1 and 91, 9; 8 | 10 | 8 |
| 1 and two coins at random1, 6, 7; 8 | 5 | 13 |