Nim

Nim is one of the oldest known mathematical games, played for centuries before Harvard mathematician Charles Bouton published a complete winning theory in 1901. Objects are arranged in heaps, and the entire game is solved through binary arithmetic, making it a favorite teaching example in game theory.

Classic Paper & Pen Medium 2 Players

Nim Rules

Set up several heaps (rows) of objects such as coins, matchsticks, or pebbles. A common starting position uses rows of 1, 3, 5, and 7 objects, but any number of heaps and sizes works.

On your turn you must remove at least one object, and you may take as many as you like — but all from a single heap in that turn. You cannot take from two heaps in one move, and you cannot skip your turn.

Players alternate until every object is gone. In normal play the person who takes the last object wins; in the more common misère version the person forced to take the last object loses.

Nim Strategy & Tips

Compute the Nim-sum

Write each heap size in binary and XOR them together. If the result is zero you are in a losing position with perfect play; if it is nonzero you can win by moving to make it zero.

Move to a zero Nim-sum

To win in normal play, always leave your opponent a position whose Nim-sum is zero. Find a heap you can reduce so the XOR of all heaps becomes zero, and take exactly that many.

Adjust for misère endings

Misère play follows the same Nim-sum rule until heaps with two or more objects are nearly gone. When only heaps of size one would remain, leave an odd number of those single-object heaps instead.

Single heap is a giveaway

If it is your move and only one nonempty heap remains, you can always win normal-play Nim by taking the whole heap. Recognizing this endgame instantly is the foundation of the full strategy.

Frequently Asked Questions

Is Nim a game of luck or skill?

Pure skill. Nim is mathematically solved, so a player who knows the Nim-sum strategy and moves first from a winning position cannot lose.

What is the Nim-sum?

The Nim-sum is the bitwise XOR of all the heap sizes in binary. A position with a Nim-sum of zero loses under perfect play, and any nonzero Nim-sum is a win for the player to move.

What is the standard starting setup?

A popular layout uses four rows of 1, 3, 5, and 7 objects, but Nim works with any number of heaps of any sizes. The strategy is identical regardless of the arrangement.

Why does the last-object rule matter?

It splits Nim into two versions. In normal play taking the last object wins, while in misère play taking it loses, and the optimal endgame differs between the two.