SMALLTALK LAB
Josephus circles: count, remove, and find the survivor
Simulate a shrinking circle, keep counting across its boundary, then replace the elimination history with a compact recurrence that predicts the last label.
Before you begin
Use a fresh Squeak Workspace; each step is a complete independent selection. Know blocks, OrderedCollection and whileTrue:. We count the current person as one, remove every kth person, and restart at the next remaining person. Unlike a ring buffer, this circle permanently loses members: it is a counting puzzle, not fixed-capacity FIFO storage.
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 - Wrap a count into a one-based position
Number seven people from 1 to 7. Start counting at person 1, with that person counted as one. To locate the third person, first convert the starting position to zero-based form, advance count minus one, take the remainder, and convert back. Squeak uses two backslashes for remainder.
| start count size |
start := 1. count := 3. size := 7.
((start+count-2) \\ size)+1Expected: 3

Try it: Set start to 6: counting 6, 7, 1 must return position 1.
02 - Remove one person and continue at the successor
Removing at position 3 shifts the old position 4 into position 3. Leave index at that position: the next round starts with its new occupant. The stored labels remain unchanged even though their collection positions move.
| people index count removed |
people := (1 to: 7) asOrderedCollection.
index := 1. count := 3.
index := ((index+count-2) \\ people size)+1.
removed := people removeAt: index.
{removed. people asArray. people at: index}Expected: #(3 #(1 2 4 5 6 7) 4)

Try it: Person 4 is next to be counted as one, not next to be eliminated.
03 - Trace the complete elimination order
Continue until exactly one person remains. Recompute the modulus using the shrinking population each round. If the last collection element was removed, index briefly equals size plus one; the next modulo expression wraps it correctly without reading that invalid position.
| people index count removed |
people := (1 to: 7) asOrderedCollection.
index := 1. count := 3. removed := OrderedCollection new.
[people size > 1] whileTrue: [
index := ((index+count-2) \\ people size)+1.
removed add: (people removeAt: index)].
{removed asArray. people first}Expected: #(#(3 6 2 7 5 1) 4)

Try it: After eliminating 6, count 7, 1, 2. Person 2 must be third in the elimination list.
04 - Make the simulation reusable
This block returns an array containing the elimination order and final label. Validate positive integer inputs before building the population. Count one removes each current starter immediately; a one-person circle performs no removals.
| simulate |
simulate := [:n :skip | | people index removed |
(n isInteger and: [n > 0]) ifFalse: [Error signal: 'Positive integer population required'].
(skip isInteger and: [skip > 0]) ifFalse: [Error signal: 'Positive integer count required'].
people := (1 to: n) asOrderedCollection.
index := 1. removed := OrderedCollection new.
[people size > 1] whileTrue: [
index := ((index+skip-2) \\ people size)+1.
removed add: (people removeAt: index)].
{removed asArray. people first}].
{simulate value: 1 value: 3. simulate value: 5 value: 1. simulate value: 5 value: 7}Expected: #(#(#() 1) #(#(1 2 3 4) 5) #(#(2 5 1 3) 4))

Try it: A count larger than the population still works because counting wraps, even several times.
05 - Reconstruct a winner in zero-based coordinates
For the winner alone, avoid storing every person. After the first removal, relabel the next person as zero. If the smaller circle has winner w, its position in the original size-m circle is (w+count) modulo m. Starting at J(1)=0, build J(m)=(J(m-1)+count) modulo m. These values describe separate circle sizes, not rounds of one simulation.
| winner positions count |
winner := 0. count := 3.
positions := OrderedCollection with: winner.
2 to: 7 do: [:size |
winner := (winner+count) \\ size.
positions add: winner].
positions asArrayExpected: #(0 1 1 0 3 0 3)

Try it: The final zero-based position is 3, which corresponds to original label 4. For size 2 the winner is position 1.
06 - Compute only the survivor
The recurrence uses a single accumulator and an increasing size. Add one only when returning the one-based label. It uses O(n) recurrence steps and O(1) auxiliary integer variables; the simulation also stores O(n) labels and can do O(n squared) shifting work when removing from an OrderedCollection.
| survivor |
survivor := [:n :skip | | winner |
(n isInteger and: [n > 0]) ifFalse: [Error signal: 'Positive integer population required'].
(skip isInteger and: [skip > 0]) ifFalse: [Error signal: 'Positive integer count required'].
winner := 0.
2 to: n do: [:size | winner := (winner+skip) \\ size].
winner+1].
{survivor value: 7 value: 3. survivor value: 5 value: 2. survivor value: 1 value: 99}Expected: #(4 3 1)

Try it: Use the simulation when you need the elimination history; use the recurrence when only the final survivor matters.
07 - Check the recurrence against the simulation
Two different algorithms should agree across many small cases. This grid covers counts above the population, one-person circles and count one. The blocks are fully included so this Workspace selection runs independently of earlier steps.
| simulate survivor |
simulate := [:n :skip | | people index removed |
(n isInteger and: [n > 0]) ifFalse: [Error signal: 'Positive integer population required'].
(skip isInteger and: [skip > 0]) ifFalse: [Error signal: 'Positive integer count required'].
people := (1 to: n) asOrderedCollection.
index := 1. removed := OrderedCollection new.
[people size > 1] whileTrue: [
index := ((index+skip-2) \\ people size)+1.
removed add: (people removeAt: index)].
{removed asArray. people first}].
survivor := [:n :skip | | winner |
(n isInteger and: [n > 0]) ifFalse: [Error signal: 'Positive integer population required'].
(skip isInteger and: [skip > 0]) ifFalse: [Error signal: 'Positive integer count required'].
winner := 0.
2 to: n do: [:size | winner := (winner+skip) \\ size].
winner+1].
(1 to: 30) allSatisfy: [:n |
(1 to: 10) allSatisfy: [:skip |
(simulate value: n value: skip) second = (survivor value: n value: skip)]]Expected: true

Try it: Changing the recurrence to winner+skip-1 should cause this check to return false.
08 - Test the contract and the count-one boundary
Zero people means there is no survivor, and a count of zero has no meaning in this counting rule. Test the precise validation messages. Also check the useful boundary law: with count one, the final label always equals the starting population.
| survivor badPopulation badCount |
survivor := [:n :skip | | winner |
(n isInteger and: [n > 0]) ifFalse: [Error signal: 'Positive integer population required'].
(skip isInteger and: [skip > 0]) ifFalse: [Error signal: 'Positive integer count required'].
winner := 0.
2 to: n do: [:size | winner := (winner+skip) \\ size].
winner+1].
badPopulation := [survivor value: 0 value: 3. false]
on: Error do: [:e | e messageText = 'Positive integer population required'].
badCount := [survivor value: 5 value: 0. false]
on: Error do: [:e | e messageText = 'Positive integer count required'].
{badPopulation. badCount.
(1 to: 20) allSatisfy: [:n | (survivor value: n value: 1) = n]}Expected: #(true true true)

Try it: Try a fractional population or count. The same integer validation must reject it before iteration.
Watch the computed states

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
The simulation index is a one-based position in a changing collection, not a permanent person label. Reusing the original population as the modulus will eventually produce wrong positions or an out-of-bounds error.
After a removal, do not increment index. The successor already moved into that position. If the removed person was last, let the next modulo calculation wrap index back into range.
The recurrence uses zero-based positions and rebuilds answers for increasing population sizes. It does not give the elimination order. Its count is the same count used in the simulation; only the coordinate system changes.
Inputs must be positive integers. A large count is valid because modulo skips complete laps. Large populations still take linear recurrence time, and the history-producing OrderedCollection simulation is intended for small teaching examples.
Three practice challenges
Challenge 1
Predict the full elimination order and survivor for five people counting every second person. Verify with the simulation.
Show solution
The removed labels are 2, 4, 1, 5 and the survivor is 3. Each new count starts at the successor of the removed person.
Challenge 2
If the count starts at label 4 instead of label 1 in the seven-person, count-three circle, how can you translate the recurrence result?
Show solution
The usual survivor is label 4, or zero-based position 3. Rotate by start minus one: ((3 + 4 - 1) \\ 7) + 1 gives label 7. Apply the rotation to the returned label, not to each recurrence size.
Challenge 3
Modify the simulation to return the remaining labels after each removal. How does this change memory use?
Show solution
After each removal, add people asArray to a snapshots collection so later mutations cannot change earlier states. Saving all shrinking populations takes O(n squared) total stored labels; saving only the elimination order takes O(n).
Continue learning
Ring Buffer | Collections | Stacks Queues
Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.