The Game of Nim


How to Play Nim

The game of nim starts with a group of nim heaps, each of which contains one or more objects, traditionally matchsticks.

Each player takes turns removing one or more objects from a single nim heap.  It is OK to remove and entire heap.

The player who picks up the last of the objects wins the game.

Suggested Configurations and Variations


How to Win at Nim

One Heap

In a game of nim where you have one nim heap, and you are allowed to remove from 1 to 4 objects at a time, you can win if you leave the heap with a multiple of 5 objects after your turn.  Thereafter, no matter what your opponent does, you can always restore the number of objects to a multiple of 5 when you take your turn.  If you cannot leave the heap with a multiple of 5 objects, then your opponent has the advantage, and only an error by your opponent can put you in a position to win the game.

More Than One Heap

In a game of nim that involves nim heaps where you can take as many objects as you want from any one of the heaps during your turn, you need to be able to compute a nim sum, that characterizes the configuration of the game. Here's how to do it:

Example of Computing a Nim Sum

Let's say you have three nim heaps, with 3, 7, and 11 matchsticks, respectively.

A Practice Problem

You are given a configuration with 3 nim heaps that have 3, 7, and 11 matchsticks respectively. What is the nim sum? If it is not 0, what can you do to make it 0?

Another Practice Problem

Consider the configuration shown to the right. There are 2 nim heaps composed of matchsticks that form each of the letters N, I, and M, for a total of 6 nim heaps all together.

Using the standard rules that allow a player to pick up 1 or more matchsticks from any one heap during each turn, which player has the advantage, the first or the second one?  What is the nim sum of this configuration? How can the player who has the advantage at the beginning of this game maintain it without going through the trouble of computing nim sums?


Nim Links