Nim: A Simple Game That Builds Mathematical Thinking

Nim is a simple two-player game that can be played anywhere. All you need is a small collection of objects, like toothpicks, marbles, rocks, or coins.

Start by arranging the objects into piles. You can make as many piles as you like, with any number of objects in each pile. A classic setup is three piles of 3, 4, and 5 objects, but there are endless possible starting arrangements.

Players take turns removing one or more objects from a single pile. A player may remove the whole pile if they want, but they cannot take objects from more than one pile in a turn.

The player who takes the last object wins.

Learning by Playing

You do not need to explicitly teach your kids anything for them to start building mathematical reasoning through nim. Simply by playing, children begin to think in terms of game states rather than single moves.

Over time, they may start picking up on patterns they cannot yet explain. Some positions feel safe. Others feel dangerous. They begin to pay attention to things like even and odd numbers. And without realizing it, they are laying the groundwork for understanding deeper mathematical concepts.

The Perfect Strategy

Nim has a perfect strategy. If you follow it, you can force a win (unless the starting position already gives the advantage to your opponent and they play perfectly).

To apply it, kids first need to know how to count in binary (base-2):

  • 1 = 001
  • 2 = 010
  • 3 = 011
  • 4 = 100
  • 5 = 101
  • 6 = 110

I have made a 2-minute video explaining how to count in base 2, which you can view on YouTube, Instagram, or Facebook.

Once kids understand this, the next step is learning the concept of XOR.

What XOR means

XOR is a special way of combining binary numbers.

Suppose you want to XOR the binary numbers 011 and 101. First, write them one above the other, with digits lined up:

011
101

Then, determine whether there is an even or odd number of 1s in each column.

  • If it's even, write a 0 below the column.
  • If it's odd, write a 1 below the column.

In the example above, the columns are odd-odd-even, so we write 110. This number is the XOR of the numbers above.

011
101
110 (XOR)

This works in the same way if you are XORing more than two numbers.

The Secret to Winning Nim: XOR 0

The perfect strategy in nim is to make sure that, after each of your turns, the piles XOR to zero.

Here's an example: Suppose the piles have 5, 4, and 2 objects, and it's your turn.

Step 1

Express the number of objects in each pile in binary.

At the top left, there are five pieces of chalk with the number 101 to the right of it. Below that are four pieces of chalk with the number 100 to the right of it. Below that are two pieces of chalk with the number 010 to the right of it.

Step 2

Determine the XOR of the piles.

At the top left, there are five pieces of chalk with the number 101 to the right of it. Below that are four pieces of chalk with the number 100 to the right of it. Below that are two pieces of chalk with the number 010 to the right of it. Under the numbers is a horizontal line. Below the line is the number 011. To the left of the number is the word XOR.

Step 3

Figure out which piece(s) you need to remove to switch XOR to zero, then do it.

In this case, you can make the piles XOR zero by removing a single piece from the pile with 2 pieces.

At the top left, there are five pieces of chalk with the number 101 to the right of it. Below that are four pieces of chalk with the number 100 to the right of it. Below that is one piece of chalk with the number 001 to the right of it. Under the numbers is a horizontal line. Below the line is the number 000. To the left of the number is the word XOR.

From there, any move your opponent makes will create a nonzero XOR, and that gives you a way to move it back to zero.

If you keep doing this, you are guaranteed to take the last object and win!

With this strategy, you can force a win. The only exception is if the piles already have an XOR of zero on your first turn and your opponent plays perfectly (i.e., always ends their turns with XOR zero).

Despite having very simple rules, Nim is a game with surprising mathematical depth. It is a great educational game for kids because it is easy enough to play right away, but complex enough to keep revealing more.