s LearnSqueak.com

SMALLTALK LAB

Chinese Remainder Theorem in Squeak: Combine Modular Clues

Turn several remainder clues into one number. Build the constructive solution, check it against every clue, and diagnose inputs outside its scope.

6 tested checkpointsNative Squeak screenshotsComputed visual replay

Before you begin

Open a Squeak Workspace. Use Print it on the complete checkpoint, including its temporary variable declarations.

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 show explicit Symbol markers: #(A B) and #(#A #B) represent the same array of Symbols.

01 - Read a remainder

Each clue describes one remainder, not a quotient. We will recover 23 from the three remainders without scanning every candidate up to 104.

Independent Workspace expression - select all, then Print it
{23 \\ 3. 23 \\ 5. 23 \\ 7}

Expected: #(2 3 2)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 1: Read a remainder
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: What would happen if you added 105 to 23?

Download this checkpoint

02 - Split the shared period

The product is the shared period for pairwise coprime moduli. Each part divides evenly by the other two moduli, so it gives zero in those slots.

Independent Workspace expression - select all, then Print it
| product |
product := 3 * 5 * 7.
{product. product // 3. product // 5. product // 7}

Expected: #(105 35 21 15)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 2: Split the shared period
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Check that 35 is divisible by 5 and 7, but not by 3.

Download this checkpoint

03 - Turn a part into a one-hot contribution

Scale 35 until its remainder in the modulus-3 slot is one; the other two slots stay zero. Multiplying this basis by the wanted remainder installs one clue without affecting the others.

Independent Workspace expression - select all, then Print it
| inverse basis |
inverse := (1 to: 2) detect: [:k | ((35 * k) \\ 3) = 1].
basis := 35 * inverse.
{inverse. basis \\ 3. basis \\ 5. basis \\ 7}

Expected: #(2 1 0 0)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 3: Turn a part into a one-hot contribution
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: The inverse search is deliberately short and transparent. The extended Euclid lesson shows how to replace it for large moduli.

Download this checkpoint

04 - Solve three clues at once

The solver creates one contribution per slot, combines them by addition, then uses remainder to choose the representative in 0 through 104. It expects equally long nonempty arrays and integer moduli greater than one.

Independent Workspace expression - select all, then Print it
| solve |
solve := [:mods :residues | | total product ok |
 product := mods inject: 1 into: [:p :m | p * m].
 total := 0. ok := true.
 1 to: mods size do: [:i | | m part inverse |
  m := mods at: i. part := product // m.
  inverse := (1 to: m - 1)
   detect: [:k | ((part * k) \\ m) = 1] ifNone: [nil].
  inverse ifNil: [ok := false] ifNotNil: [
   total := total + ((residues at: i) * part * inverse)]].
 ok ifTrue: [total \\ product] ifFalse: [#notCoprime]].
solve value: #(3 5 7) value: #(2 3 2)

Expected: 23

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 4: Solve three clues at once
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Save the expected result, then verify each clue separately.

Download this checkpoint

05 - Reorder or normalize the same clues

Reordering whole clues does not alter a simultaneous solution. A negative representative for a remainder can encode the same clue: −1 differs from 2 by a multiple of 3.

Independent Workspace expression - select all, then Print it
| solve |
solve := [:mods :residues | | total product ok |
 product := mods inject: 1 into: [:p :m | p * m].
 total := 0. ok := true.
 1 to: mods size do: [:i | | m part inverse |
  m := mods at: i. part := product // m.
  inverse := (1 to: m - 1)
   detect: [:k | ((part * k) \\ m) = 1] ifNone: [nil].
  inverse ifNil: [ok := false] ifNotNil: [
   total := total + ((residues at: i) * part * inverse)]].
 ok ifTrue: [total \\ product] ifFalse: [#notCoprime]].
{solve value: #(5 7 3) value: #(3 2 2).
 solve value: #(3 5 7) value: #(-1 3 2)}

Expected: #(23 23)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 5: Reorder or normalize the same clues
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Swap just two moduli without swapping their residues too. A different result is not a bug.

Download this checkpoint

06 - Reject noncoprime moduli

This version rejects every noncoprime pair because a required inverse is missing. The two input sets illustrate different reasons not to guess: 9 satisfies the first, but odd versus even makes the second impossible.

Independent Workspace expression - select all, then Print it
| solve |
solve := [:mods :residues | | total product ok |
 product := mods inject: 1 into: [:p :m | p * m].
 total := 0. ok := true.
 1 to: mods size do: [:i | | m part inverse |
  m := mods at: i. part := product // m.
  inverse := (1 to: m - 1)
   detect: [:k | ((part * k) \\ m) = 1] ifNone: [nil].
  inverse ifNil: [ok := false] ifNotNil: [
   total := total + ((residues at: i) * part * inverse)]].
 ok ifTrue: [total \\ product] ifFalse: [#notCoprime]].
{solve value: #(4 6) value: #(1 3).
 solve value: #(4 6) value: #(1 2)}

Expected: #(notCoprime notCoprime)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 6: Reject noncoprime moduli
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: A generalized CRT must check agreement modulo the common divisor before solving.

Download this checkpoint

Replay the checkpoints

NATIVE SQUEAK CAPTURE
Chinese Remainder Theorem in Squeak: Combine Modular Clues native Workspace replay
Replay of successive native Squeak Workspace evaluations, not an interaction recording. The complete static checkpoints appear above.

Reason about correctness and limits

Any two solutions differ by a multiple of every modulus. For pairwise coprime moduli that forces a difference divisible by their product. This is why selecting one representative modulo 105 is unambiguous.

Linear inverse search is intentionally simple for the tiny inputs. Validate array lengths and bounds if exposing the block to arbitrary user input.

Three practice challenges

One clue

What should solve give for modulus 7 and residue 9?

Show solution

2, the normalized representative modulo 7.

Build a different basis

What are the remainders of 21 modulo 3, 5 and 7?

Show solution

0, 1, 0. For this clue, the complementary part is already a basis.

Lift the result

Find the next nonnegative solution after 23.

Show solution

128, by adding the shared period of 105.

Continue learning

Extended Euclid | Euclidean Gcd | Integer Bitsets

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