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.
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.
{23 \\ 3. 23 \\ 5. 23 \\ 7}Expected: #(2 3 2)

Try it: What would happen if you added 105 to 23?
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.
| product |
product := 3 * 5 * 7.
{product. product // 3. product // 5. product // 7}Expected: #(105 35 21 15)

Try it: Check that 35 is divisible by 5 and 7, but not by 3.
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.
| 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)

Try it: The inverse search is deliberately short and transparent. The extended Euclid lesson shows how to replace it for large moduli.
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.
| 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

Try it: Save the expected result, then verify each clue separately.
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.
| 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)

Try it: Swap just two moduli without swapping their residues too. A different result is not a bug.
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.
| 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)

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

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.