15/28
Part 4 of 7 · Count the waysLearn

Amounts outside: any coin can be last, so orderings count

Your orderings function put the loops the other way round. Here is the table it fills:

Orderings, any coin lastInteractive
Coins: 1, 3, 4. Tap a cell to see where its number comes from.
P(6) = P(5) + P(3) + P(2) = 6 + 2 + 1 = 9. Any coin can come last.

With the amount loop outside, any coin can be the last one at any amount:

P(t)=∑c ≤ tP(t−c)P(t) = \sum_{c \,\le\, t} P(t - c)

P(t)P(t) counts the sequences that make tt, grouped by their last coin. So 1 + 1 + 4 and 1 + 4 + 1 end differently, and they count twice.

For the fewest coins, the loop order doesn't matter. A minimum doesn't care what order the coins arrived in, and both orders fill the same table. Only counting depends on it.