SMALLTALK LAB
Count coin-change combinations in Squeak
Build an array of reachable totals and count unordered combinations with unlimited coins.
Before you begin
Open a Workspace in native Squeak and use Print it on one complete checkpoint at a time.
These are native Squeak 6.1 examples. Each checkpoint is independent: open the download as text, copy one complete expression into Workspace and choose Print it. Do not file in the checkpoint collection as a class definition.
Expected values are Smalltalk expressions. The printer may display equivalent Smalltalk literals with different spaces or delimiters.
01 - Seed one way to make zero
Array index one represents amount zero. Paying zero with no coins counts as one empty combination; positive totals start at zero ways.
| counts |
counts := Array new: 5 withAll: 0.
counts at: 1 put: 1.
countsExpected: #(1 0 0 0 0)

Try it: What amount does array index 4 represent?
02 - Introduce a coin of value one
Walking up from amount one allows each amount to inherit the preceding count. Every total now has one all-ones solution.
| ways |
ways := [:coins :target |
| counts |
target < 0 ifTrue: [Error signal: 'Negative target'].
((coins anySatisfy: [:c | c isInteger not or: [c <= 0]])
or: [coins asSet size ~= coins size])
ifTrue: [Error signal: 'Use distinct positive integers'].
counts := Array new: target + 1 withAll: 0.
counts at: 1 put: 1.
coins do: [:coin |
coin to: target do: [:sum |
counts at: sum + 1 put:
((counts at: sum + 1) + (counts at: sum - coin + 1))]].
counts].
ways value: #(1) value: 5Expected: #(1 1 1 1 1 1)

Try it: Amount 5 is the last entry, at index 6.
03 - Add the coin of value two
For each sum, add ways to make sum minus two. That earlier entry has already processed this denomination, so two can be used more than once.
| ways |
ways := [:coins :target |
| counts |
target < 0 ifTrue: [Error signal: 'Negative target'].
((coins anySatisfy: [:c | c isInteger not or: [c <= 0]])
or: [coins asSet size ~= coins size])
ifTrue: [Error signal: 'Use distinct positive integers'].
counts := Array new: target + 1 withAll: 0.
counts at: 1 put: 1.
coins do: [:coin |
coin to: target do: [:sum |
counts at: sum + 1 put:
((counts at: sum + 1) + (counts at: sum - coin + 1))]].
counts].
ways value: #(1 2) value: 5Expected: #(1 1 2 2 3 3)

Try it: At amount 4 there are three ways: use zero, one, or two coins of value 2.
04 - Count a shortcut without counting orders
Adding denomination five introduces one new way to pay five. Putting denomination on the outside means 2+1 and 1+2 do not become separate payments.
| ways |
ways := [:coins :target |
| counts |
target < 0 ifTrue: [Error signal: 'Negative target'].
((coins anySatisfy: [:c | c isInteger not or: [c <= 0]])
or: [coins asSet size ~= coins size])
ifTrue: [Error signal: 'Use distinct positive integers'].
counts := Array new: target + 1 withAll: 0.
counts at: 1 put: 1.
coins do: [:coin |
coin to: target do: [:sum |
counts at: sum + 1 put:
((counts at: sum + 1) + (counts at: sum - coin + 1))]].
counts].
ways value: #(1 2 5) value: 5Expected: #(1 1 2 2 3 4)

Try it: Write the four combinations that reach amount 5.
05 - Show a target that cannot be paid
An even denomination never creates an odd total from zero. Amount four has two combinations, but amount five has none.
| ways |
ways := [:coins :target |
| counts |
target < 0 ifTrue: [Error signal: 'Negative target'].
((coins anySatisfy: [:c | c isInteger not or: [c <= 0]])
or: [coins asSet size ~= coins size])
ifTrue: [Error signal: 'Use distinct positive integers'].
counts := Array new: target + 1 withAll: 0.
counts at: 1 put: 1.
coins do: [:coin |
coin to: target do: [:sum |
counts at: sum + 1 put:
((counts at: sum + 1) + (counts at: sum - coin + 1))]].
counts].
ways value: #(2 4) value: 5Expected: #(1 0 1 0 2 0)

Try it: Replace target 5 with 6; expect two ways at the final entry.
06 - Verify the denomination order does not matter
The intermediate rows depend on the order we introduce denominations, but the final combinations are the same as long as each denomination appears exactly once.
| ways |
ways := [:coins :target |
| counts |
target < 0 ifTrue: [Error signal: 'Negative target'].
((coins anySatisfy: [:c | c isInteger not or: [c <= 0]])
or: [coins asSet size ~= coins size])
ifTrue: [Error signal: 'Use distinct positive integers'].
counts := Array new: target + 1 withAll: 0.
counts at: 1 put: 1.
coins do: [:coin |
coin to: target do: [:sum |
counts at: sum + 1 put:
((counts at: sum + 1) + (counts at: sum - coin + 1))]].
counts].
(ways value: #(5 2 1) value: 5) = (ways value: #(1 2 5) value: 5)Expected: true

Try it: Try the order #(2 1 5) as well.
Replay the checkpoints

Reason about correctness and limits
Use a nonnegative integer target and distinct positive integer coin values. Duplicate denominations would count equivalent combinations more than once.
This counts combinations, not ordered payment sequences and not minimum coins. The outer loop introduces one denomination at a time.
The array uses O(target) slots and the program performs O(denominations × target) additions. Large exact integers can make individual additions more expensive.
Three practice challenges
List all four payments
How can you make five with 1, 2 and 5?
Show solution
1+1+1+1+1; 2+1+1+1; 2+2+1; 5.
Notice an impossible total
Why is five unreachable with only 2 and 4?
Show solution
Every allowed coin is even, so every sum is even.
Switch the question
Would an outer loop over amounts still count these combinations?
Show solution
Not in general. It can count sequences that end with different coins separately, so it can distinguish 1+2 from 2+1.
Reference
Carnegie Mellon notes: contrast the ordered-sequence variant §5.2
Continue learning
Knapsack | First Occurrences Of
Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.