Quest2GameRead the game. Change the rules.

Lesson · 6 min Gentle

The Game of Nim

In one line In take-away games you can count back from the end to find safe positions, and then keep landing on them.

Some games can be worked backward all the way to the start, by hand. Take the race to 21, played in schoolyards in many countries. Two players take turns counting upward, each adding one, two or three to the last number said. Whoever says 21 wins.

Start at the end. If you say 17, your opponent must say 18, 19 or 20, and whichever it is, you can reach 21. So 17 is a safe number. Step back four: if you say 13, the opponent can reach 14, 15 or 16, and from any of those you can reach 17. The same goes for 9, for 5 and for 1. So the first player can force a win: she says 1, and then adds whatever makes four with the opponent's step. If they add one, she adds three; if they add two, she adds two.

Nim works the same way. Players take turns removing matches, stones or beans from heaps. On each turn you take as many as you like, but from one heap only, and in the usual version whoever takes the last one wins. With two equal heaps, the second player is safe: whatever the first takes from one heap, take the same from the other, and the heaps stay equal to the end. With unequal heaps, the first player levels them and becomes the copier. In 1901 the American mathematician Charles Bouton published a rule for any number of heaps, which takes more counting but works in the same spirit.

The idea in all of these is the safe position: one that, once you reach it, you can keep reaching however the opponent plays. Whether the first player can force a win depends on where the game starts. The next question is what happens when a game is far too big to count back through.

Beans in Oaxaca

Illustration: Beans in Oaxaca

Cuauhtémoc, who is eleven and lives in Oaxaca, kept losing a game of beans to his grandmother. She set out two heaps on the kitchen table, he took any number from one heap, then she did, and whoever took the last bean won. She let him choose who went first, and somehow she won anyway.

One evening he stopped counting beans and watched her hands. Whenever the heaps were unequal on her turn, she took from the bigger one until they matched. Whenever they were equal on his turn, he was stuck: anything he took, she took the same from the other heap. So the next night, with heaps of seven and four, he asked to go first and took three from the seven. His grandmother looked at the two heaps of four, smiled, and said it was time he learned to wash the pot.

Try this today Play the race to 21 with someone today. Go first, say 1, and keep each pair of turns adding up to four.

Question 1 of 2

In a race to 21 played on a bus in Durban, Andile has just said 13. Her friend will now add one, two or three.

Which number should Andile aim to say next?

Question 2 of 2

Two heaps of seven stones lie on a step in Marrakech, and Bakhyt may choose to go first or second. Each turn takes any number from one heap; whoever takes the last stone wins.

Which seat should Bakhyt choose?

Cite or share this lesson

A plain reference you can paste into notes or an essay:

Quest2Game. “The Game of Nim.” https://quest2game.com/learn/lesson/the-game-of-nim. Accessed 30 November 2026.

Sources
  • Nim — The game of heaps and Charles Bouton's rule for winning it.
  • Combinatorial game theory — The study of games of turns with no chance and no hidden information.