s LearnSqueak.com

SMALLTALK LAB

Pascal triangle: build combinations from neighboring sums

Construct binomial rows with arrays, verify symmetry and powers-of-two totals, and use Pascal’s triangle to count combinations without factorials.

8 tested checkpointsNative Squeak screenshotsComputed visual replay

Before you begin

Know Arrays, loops and blocks. Row zero is #(1). Each new row begins and ends with one; every interior cell adds the two cells above it.

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.

01 - Start with row zero

The top row has one entry. Numbering it zero makes row n contain n+1 entries, matching the coefficients of (a+b) raised to n.

Independent Workspace expression - select all, then Print it
| row |
row := #(1).
row size

Expected: 1

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 1: Start with row zero
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Row zero represents the constant polynomial 1.

Download this checkpoint

02 - Compute a single interior sum

For row #(1 3 3 1), the next middle entry adds the two threes. Smalltalk indexing starts at one even though our mathematical row number starts at zero.

Independent Workspace expression - select all, then Print it
| row |
row := #(1 3 3 1).
(row at: 2)+(row at: 3)

Expected: 6

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 2: Compute a single interior sum
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: The adjacent sums at the edges are 1+3 and 3+1.

Download this checkpoint

03 - Build the next row in fresh storage

Allocate one extra cell, initialize the boundary ones, then fill interior positions. Do not update the old row in place: later sums must still see its original values.

Independent Workspace expression - select all, then Print it
| row next |
row := #(1 3 3 1).
next := Array new: row size+1 withAll: 1.
2 to: row size do: [:i |
 next at: i put: (row at: i-1)+(row at: i)].
next

Expected: {1. 4. 6. 4. 1.}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 3: Build the next row in fresh storage
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Check that row is still #(1 3 3 1).

Download this checkpoint

04 - Package one transition

This block has one responsibility: return the row after the supplied nonempty valid Pascal row. A one-element row has no interior loop iterations and produces two boundary ones.

Independent Workspace expression - select all, then Print it
| nextRow |
nextRow := [:previous | | next |
 next := Array new: previous size+1 withAll: 1.
 2 to: previous size do: [:i |
  next at: i put: (previous at: i-1)+(previous at: i)].
 next].
nextRow value: #(1)

Expected: {1. 1.}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 4: Package one transition
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Apply it again to get #(1 2 1).

Download this checkpoint

05 - Advance to a requested row

Starting at row zero, execute exactly n transitions. The resulting array is row n. This is iterative dynamic programming: reuse the previous result rather than recursively recompute shared subproblems.

Independent Workspace expression - select all, then Print it
| nextRow row n |
nextRow := [:previous | | next |
 next := Array new: previous size+1 withAll: 1.
 2 to: previous size do: [:i |
  next at: i put: (previous at: i-1)+(previous at: i)].
 next].
n := 5. row := #(1).
n timesRepeat: [row := nextRow value: row].
row

Expected: {1. 5. 10. 10. 5. 1.}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 5: Advance to a requested row
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: For n = 0 no transitions occur. Validate n as a nonnegative integer in a public API.

Download this checkpoint

06 - Read a combination count

The entry at mathematical column k counts ways to choose k objects from n. Translate column k to Smalltalk index k+1. There are ten pairs among five distinct objects.

Independent Workspace expression - select all, then Print it
| nextRow row |
nextRow := [:previous | | next |
 next := Array new: previous size+1 withAll: 1.
 2 to: previous size do: [:i |
  next at: i put: (previous at: i-1)+(previous at: i)].
 next].
row := #(1).
5 timesRepeat: [row := nextRow value: row].
row at: 2+1

Expected: 10

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 6: Read a combination count
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Column zero and column five both count one choice.

Download this checkpoint

07 - Check symmetry and row totals

Every row equals its reversal. Its entries sum to 2 raised to n, because every subset either includes or excludes each of n objects. Test both properties through row twelve.

Independent Workspace expression - select all, then Print it
| nextRow row valid |
nextRow := [:previous | | next |
 next := Array new: previous size+1 withAll: 1.
 2 to: previous size do: [:i |
  next at: i put: (previous at: i-1)+(previous at: i)].
 next].
row := #(1). valid := true.
0 to: 12 do: [:n |
 valid := valid and: [row = row reversed and: [row sum = (2 raisedTo: n)]].
 row := nextRow value: row].
valid

Expected: true

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 7: Check symmetry and row totals
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Change one interior entry to see which property detects the corruption.

Download this checkpoint

08 - Use the row as polynomial coefficients

Row three is #(1 3 3 1). Weight coefficient k by x raised to k. At x=2 the sum is 27, equal to (1+2) cubed. This connects counting with algebra.

Independent Workspace expression - select all, then Print it
| row total |
row := #(1 3 3 1). total := 0.
0 to: 3 do: [:k | total := total+((row at: k+1)*(2 raisedTo: k))].
total

Expected: 27

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 8: Use the row as polynomial coefficients
Actual native Squeak Workspace after Print it. The selected answer was evaluated from this code.

Try it: Try x = 1: the result is eight, the row total.

Download this checkpoint

Watch the computed states

NATIVE SQUEAK CAPTURE
Pascal triangle: build combinations from neighboring sums native visual trace
Replay of actual Morphic window captures at successive computed stages; not mouse footage.

Download the visual experiment. Select the entire expression and Do it. Change stage := 0. to another supported stage, close the previous window, and run again. The GIF replays those native states. This example needs native Squeak; no new browser playground compatibility is claimed.

Reason about correctness and limits

Computing row n this way performs O(n squared) additions and retains O(n) entries when only the current row is kept. Displaying every row requires more storage. Integer sizes grow, so arithmetic is not constant cost for very large n.

The transition assumes a nonempty Pascal row. It is not a general validator: feeding arbitrary arrays produces neighbor sums but not necessarily binomial coefficients.

Choose counts use zero-based mathematical k but one-based at: indices. Guard 0 <= k <= n when exposing a general choose: API.

Three practice challenges

Challenge 1

Why can you generate only one row at a time?

Show solution

The recurrence uses only the immediately preceding row, not the whole triangle.

Challenge 2

What is the sum of row eight?

Show solution

2 raised to 8, or 256, because it counts all subsets of eight objects.

Challenge 3

Why do the two boundary entries stay one?

Show solution

There is exactly one way to choose none of the objects and exactly one way to choose them all.

Continue learning

Collections | Fractions | Knapsack

Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.