All games

How to Win Nim

Nim is solved: the player in a winning position can always win. Here’s how for each version.

GameHow to win
Take 1 to 3The 21 GameMake each round add up to 4Leave a multiple of 4 (+1 if the last stone loses)
Two rowsMake them equal, then copy your opponent in the other row
Three or more rowsNim, 1-3-5-7Split 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.

  1. Your opponent goes first. 21 is 1 more than a multiple of 4, so you, moving second, can win
  2. Opponent takes 2. 19 left
  3. You take 2 (4 together). 17 left
  4. Opponent takes 3. 14 left
  5. You take 1 (4 together). 13 left
  6. Opponent takes 1. 12 left
  7. You take 3. 9 left
  8. Opponent takes 2. 7 left
  9. You take 2. 5 left
  10. Opponent takes 3. 2 left
  11. You take 1. 1 left
  12. 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 takeLast stone loses: leaveLast stone wins: leave
1–21 more than a multiple of 31, 4, 7, 10…Multiples of 33, 6, 9, 12…
1–31 more than a multiple of 41, 5, 9, 13…Multiples of 44, 8, 12, 16…
1–41 more than a multiple of 51, 6, 11, 16…Multiples of 55, 10, 15, 20…
1–51 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.

  1. 5 and 3, and the last stone wins. You go first
  2. Take 2 from the top row to make 3 and 3
  3. Opponent takes 2 from the top row: 1 and 3
  4. Take the same 2 from the bottom row: 1 and 1
  5. Opponent takes the stone in the top row
  6. 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.

Stones421
Row 13●●
Row 24●
Row 35●●
Unpaired2●

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)

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.

Row 110001
Row 230011
Row 350101
Row 470111
Nim-sum00000
Last stone
The nim-sum is 0 (0000). The player to move loses if the opponent plays correctly (from the starting position, the second player wins).

When 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.

  1. 3-4-5 and the last stone loses. The nim-sum is 2, so you can win by moving first
  2. Take 2 from row 1: 1-4-5. Nim-sum 0
  3. Opponent takes 3 from row 3: 1-4-2. Nim-sum 7
  4. Take 1 from row 2: 1-3-2. Nim-sum 0
  5. Opponent takes all of row 2: 1-0-2. Nim-sum 3
  6. Leaving 1-0-1 would make you take the last stone, so take all of row 3 to leave one 1-stone row
  7. 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.

  1. Puzzle 1: rows of 1, 2 and 4

    Show answer

    Take 1 from the row of 4 to leave 1-2-3 (remove the 4, add the unpaired 2 and 1).

  2. Puzzle 2: rows of 2, 5 and 6

    Show answer

    Take 1 from the row of 5 (4 + 1) to leave 2-4-6. Only the 1 is unpaired.

  3. Puzzle 3: rows of 1, 1 and 3

    Show 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.
Play Nim (free)