How to Win Nim
Nim is solved: the player in a winning position can always win. Here’s how for each version.
| Game | How to win |
|---|---|
| Take 1 to 3The 21 Game | Make each round add up to 4Leave a multiple of 4 (+1 if the last stone loses) |
| Two rows | Make them equal, then copy your opponent in the other row |
| Three or more rowsNim, 1-3-5-7 | Split rows into 4s, 2s and 1s. Leave no unpaired part (nim-sum 0) |
With two or more rows and “last stone loses”, only the endgame is different.
Play Nim (free)The 21 Game: leave 17, 13, 9, 5, then 1
Take 1 to 3 from 21 stones. Whoever takes the last stone loses. Make each round add up to 4.
- Your opponent goes first. 21 is 1 more than a multiple of 4, so you, moving second, can win
- Opponent takes 2. 19 left
- You take 2 (4 together). 17 left
- Opponent takes 3. 14 left
- You take 1 (4 together). 13 left
- Opponent takes 1. 12 left
- You take 3. 9 left
- Opponent takes 2. 7 left
- You take 2. 5 left
- Opponent takes 3. 2 left
- You take 1. 1 left
- Your opponent has to take the last stone. You win
Start (11 moves)
If taking the last stone wins, leave a multiple of 4 instead (with 21 stones, take 1 first).
| You may take | Last stone loses: leave | Last stone wins: leave |
|---|---|---|
| 1–2 | 1 more than a multiple of 31, 4, 7, 10… | Multiples of 33, 6, 9, 12… |
| 1–3 | 1 more than a multiple of 41, 5, 9, 13… | Multiples of 44, 8, 12, 16… |
| 1–4 | 1 more than a multiple of 51, 6, 11, 16… | Multiples of 55, 10, 15, 20… |
| 1–5 | 1 more than a multiple of 61, 7, 13, 19… | Multiples of 66, 12, 18, 24… |
Two rows: make them equal, then copy
If taking the last stone wins, just take as many from the other row as your opponent did.
- 5 and 3, and the last stone wins. You go first
- Take 2 from the top row to make 3 and 3
- Opponent takes 2 from the top row: 1 and 3
- Take the same 2 from the bottom row: 1 and 1
- Opponent takes the stone in the top row
- Take the last stone. You win
Start (5 moves)
Three or more rows: make the nim-sum 0
Split each row into 4s, 2s and 1s (and 8s from 8 up). If every part has a partner in another row, you’ve left a winning position.
| Stones | 4 | 2 | 1 | |
|---|---|---|---|---|
| Row 1 | 3 | ● | ● | |
| Row 2 | 4 | ● | ||
| Row 3 | 5 | ● | ● | |
| Unpaired | 2 | ● |
A 2 is left over, so take 2 from row 1 to leave 1-4-5.
That leftover is the nim-sum: the rows in binary, added without carrying (bitwise XOR).
Positions to leave (nim-sum 0)
- 1-2-3
- 1-4-5
- 2-4-6
- 2-5-7
- 3-4-7
- 3-5-6
- 1-3-5-7
- Any two equal rows
Winning move calculator
Set each row with + and − to see the nim-sum and every winning move. The 0s and 1s mark the 8, 4, 2 and 1 in a row.
00010011010101110000When the last stone loses: leave an odd number of 1-stone rows
Play for nim-sum 0 as usual. Once only one row has 2 or more stones, take all of it or leave 1 instead.
- 3-4-5 and the last stone loses. The nim-sum is 2, so you can win by moving first
- Take 2 from row 1: 1-4-5. Nim-sum 0
- Opponent takes 3 from row 3: 1-4-2. Nim-sum 7
- Take 1 from row 2: 1-3-2. Nim-sum 0
- Opponent takes all of row 2: 1-0-2. Nim-sum 3
- Leaving 1-0-1 would make you take the last stone, so take all of row 3 to leave one 1-stone row
- Your opponent has to take the last stone. You win
Start (6 moves)
Practice
It’s your move in each, and the last stone loses.
Puzzle 1: rows of 1, 2 and 4
124Show answer
Take 1 from the row of 4 to leave 1-2-3 (remove the 4, add the unpaired 2 and 1).
Puzzle 2: rows of 2, 5 and 6
256Show answer
Take 1 from the row of 5 (4 + 1) to leave 2-4-6. Only the 1 is unpaired.
Puzzle 3: rows of 1, 1 and 3
113Show answer
Take 2 from the row of 3 to leave 1-1-1. Taking all 3 leaves two 1-stone rows, and you’d have to take the last stone.
FAQ
- Is it better to go first or second?
- If the starting nim-sum is 0, the second player wins. Otherwise the first player wins (with perfect play).
- Who wins the 21 Game?
- The second player if the last stone loses, the first player if it wins.
- How do I win 1-3-5-7?
- Let your opponent go first, then make the nim-sum 0 again (if they leave 1-3-5-4, take all 3 to leave 1-0-5-4).
- What if more than one part is unpaired (like 1-3-5-1)?
- Use the row with the largest unpaired part. Remove the unpaired parts it has and add the ones it lacks (5 becomes 3).
- Two rows and the last stone loses?
- Copy as usual. When your opponent leaves one row with 1 stone or none, take all of the other row or leave 1, so only one 1-stone row remains.
- Is the “don’t say 21” counting game the same?
- Yes. Players say 1 to 3 numbers in turn. Say 4, 8, 12, 16 and 20 yourself to win.
- What is the nim-sum?
- The total of the 8s, 4s, 2s and 1s that don’t pair up. It is the bitwise XOR of the rows in binary.
- Why does the nim-sum tell who wins?
- From nim-sum 0, every move makes it non-zero. From non-zero, some move always makes it 0.
- What about Wythoff’s game (you may also take the same number from both rows)?
- It needs a different strategy. If the last stone wins, leave (1,2), (3,5), (4,7), (6,10) or (8,13).
- Does the calculator work for the 21 Game?
- No. It is for games where you may take any number from one row.
- How do I beat the Hard computer?
- Pick a layout with nim-sum 0, such as 1-3-5-7, and press “Computer first” so it moves first.