A 3,000-year-old game

Nim

Take turns removing stones from one row. Take the last stone and you win. The computer plays perfectly every turn, but who goes first alternates each game. Know the trick, and you can win.

Your move
Drag the empty sand to look around
🏆

You win!

The winning trick: the nim-sum

The computer isn’t guessing. It uses a piece of math more than a century old (Charles Bouton, 1901) built on binary numbers.

1

Write each pile in binary

Break every pile into place values of 16, 8, 4, 2 and 1. A pile of 5 is 4 + 1, so it’s 00101.

2

Count the 1s in each column

If every column has an even number of 1s, the position is balanced. If any column is odd, it’s unbalanced. Writing a 1 under each odd column gives the nim-sum (programmers call it XOR).

3

Always hand your opponent a balanced position

On your turn, take stones from one pile so that every column becomes even again. Keep doing that and you will take the last stone.

Your game right now

Example: piles of 3, 4 and 5

pile 3 = 0 1 1 pile 4 = 1 0 0 pile 5 = 1 0 1 1s = 2 1 2 ← the 2s column is odd: unbalanced Fix it: change pile 3 (011) to 001 by taking 2 stones. Now 1, 4, 5 = 001, 100, 101 → every column even.

Why it works

Why can you ever win? Who goes first alternates. If you start from a balanced position and the computer has to move first, it can’t fix it. Keep it balanced and the last stone is yours. You can also pick your own piles: try a balanced setup like 1, 2, 3 or 3, 5, 6.