Idea: Start from the end
Label each position winning or losing for the player to move, from size upward.
- No moves left: losing, when the last item wins
- Winning: some move leads to a losing position
- Losing: every move leads to a winning position
Formula: Take-away games
With moves of 1 to k, the losing sizes are the multiples of k + 1; answer a move a with k + 1 − a.
Example: 21 matches
matches; each turn take , or ; whoever takes the last wins. Go first? How many do you take?
- Losing sizes are multiples of ; answer each with
- , so go first
- Leave : take
Common mistake: Misreading who wins
Using the same table when taking the last item loses. The losing sizes shift.
Instead Then leave more than a multiple of , not a multiple. Read the winning rule twice.
Idea: Unusual move sets
The table works for any set of moves, even where no neat formula exists.
- Moves , , : losing sizes , , , , ,
- Remainder or on division by , as
- Find the period by filling the table, never by guessing
Quick check: Count the losing sizes
With moves of , or , how many pile sizes from to are losing for the player to move?
Show the answer
Answer:
Losing sizes leave remainder or on division by : from up to .
Formula: Nim: the binary sum
Add heap sizes in binary without carrying; the position is losing exactly when that sum is 0.
Example: Heaps of 3, 4 and 5
Nim with heaps , , : take any number from one heap; the last item wins. How many do you take?
- Binary , , : the sum is
- Flip the middle bit of : , which is
- Take from the first heap
Idea: Say it in the room
State the losing positions first, then the move that reaches one.
- Take-away games: give the period and the reply rule
- Nim: say the binary sums column by column
- That column step is where slips happen
Courses / Brainteaser patterns