PlayNook

Math & Logic · Binary

Nim

Take as many marks as you like from one row. Whoever takes the last one wins — or loses, if you agree that first. One line of binary decides every position, and the table on this page shows it live.

Rows of marks. On your turn, cross out as many as you like from one row — one, several, the whole row. Whoever takes the last mark wins.

That is the entire game, and it has been completely solved since 1901. Not solved in the sense of a computer worked it out — solved in the sense that one line of binary tells you the right move in every position that will ever arise, and you can learn it in about four minutes.

How to play Nim

Nim is a two-player game. Play a friend opens a table with a short code; send the code to anyone and they join from their own device, with a chat box for the arguing. Computer is the solo version, with three opponents.

One deliberate difference between them: against the computer the binary table starts open, because that is what the page is for and the machine knows the answer anyway. Against a person it starts closed — whoever can see it wins, and two people both reading it are not playing a game. The button is still there if you want to learn it together.

The table beside the board

A solved game makes a thin opponent. Set the computer to Perfect and it cannot be beaten from a position that was lost when you arrived at it; set it lower and you are playing against something deliberately worse.

What is worth your time is the binary table. It shows each row in binary, the count of ones in each column, and the one column that has gone odd — which is where the winning move lives. Play a few games watching it and the strategy stops being a rule you remember and becomes something you can see.

Then open a table, send someone the code, leave the table closed, and find out whether you actually learned it.

Nim strategy: how to find the winning move

Rows of 3, 4 and 5 written in binary with the twos column odd, and the same position after taking two from the row of three, with every column even
Generated by the same engine that runs the game. One odd column, one winning move, and an all-even table to hand over.

Write each row length in binary, one under the other. Count the ones in each column. If every column is even, whoever is to move has already lost. If some column is odd, there is at least one winning move.

To find it: look at the leftmost odd column, pick a row with a 1 there, and reduce that row until every column comes out even. There is always such a move, and the arithmetic is easier than it sounds — the target size for that row is the exclusive-or of all the other rows.

Then do it again next turn, and every turn. Your opponent is handed an all-even table each time, and from an all-even table every possible move breaks some column. They cannot help but hand it back broken.

Why 1-3-5-7 is already lost for whoever starts

The board everybody draws is not a neutral starting point.

The 1-3-5-7 rows written in binary, with every column containing an even number of ones, beside the same position drawn as rows of marks
1, 3, 5 and 7 in binary. Every column is even, so the nim-sum is zero — and zero means the player about to move has no winning move at all.

1 ⊕ 3 ⊕ 5 ⊕ 7 = 0. The classic arrangement is a lost position for the player who goes first, which puts the traditional courtesy of offering your opponent the first move in a rather different light.

If you want a fair game, use 3-4-5, or any set of rows whose nim-sum is not zero. Both are on the page.

Misère Nim: the ending that catches everyone

Agree beforehand whether taking the last mark wins or loses. The version where it loses is misère Nim — the game played in Last Year at Marienbad, which is why it turns up under that name.

The good news is that you barely have to learn anything new. The surprising part is how little.

A bar showing 2,385 of 2,400 positions behaving identically under both rules and 15 differing, with the fifteen shown to be positions of single marks only
Every position with up to four rows of up to six marks, checked in both versions against a full game-tree search. Only fifteen behave differently — and all of them are made of single marks.

Of the 2,400 positions we checked, 2,385 behave identically under both rules. Only 15 differ, and every one of those consists of nothing but rows of a single mark.

So the rule is: play misère Nim exactly like normal Nim — the nim-sum still decides everything — right up until no row has more than one mark left. At that moment the rule inverts. With only single marks on the table, you want to leave your opponent an odd number of them, because they must take one each turn and the last one is fatal.

That single-sentence exception is why almost everybody plays misère Nim correctly for twenty moves and then loses it in the last three.

The strategy most people know is only half of one

Ask someone who has played Nim what the trick is and you will usually hear: get the rows into equal pairs, then copy whatever the other player does. That is genuinely part of the truth, and it works surprisingly well — but only in one of the two versions.

Measured win rates from already-won positions: careless about 1%, pairs-only 51.5% in the normal game but 0.3% in misère, perfect 100% in both
Four thousand games per row, always starting from a position that was theoretically won, always against a perfect opponent. The pairing player is a real player, not noise.

We built that player and measured it. Starting from positions that were already won, the pairing strategy converts 51.5% of them in the normal game — a real, useful half-theory.

In misère it converts 0.3%, which is no better than moving at random.

The reason is exactly the exception above. Pairing carries you faultlessly to the endgame and then walks you straight into it: the last few single marks are precisely where the misère rule flips, and pairing has nothing to say about them. It is a good illustration of a general hazard — a rule of thumb that works can be worse than no rule at all, because it gets you confidently to the place where it fails.

Why every impartial game is secretly Nim

This is the reason Nim is in the textbooks rather than the toy shops.

An impartial game is one where both players have exactly the same moves available from any position, and nothing is hidden or random. Nim is impartial. So is a game where you remove coins under some other restriction, and so are a great many others.

The Sprague–Grundy theorem — found independently by Roland Sprague in 1935 and Patrick Grundy in 1939 — says that every impartial game position is equivalent to a single heap of Nim of some size. Not similar to: equivalent to. Work out that size, and everything you know about Nim transfers over wholesale, including the nim-sum trick for adding several games together.

Nim is therefore not one solved game among many. It is the thing all the others turn out to be.

Who invented Nim?

The game is old and its history is mostly unwritten; versions of it were played in China and around Europe long before anybody explained it. What is precisely dated is the solution: Charles Bouton, at Harvard, published Nim, a game with a complete mathematical theory in 1901. He gave it the name too, and suggested it comes from the German nimm — take.

In 1940 the Westinghouse company built the Nimatron, a relay machine that played Nim against visitors at the New York World's Fair. It was one of the earliest game-playing machines anybody built, and it won most of the time, which is not surprising: it knew the one thing there is to know.

If you would like the version with a pencil, there are printable Nim sheets — and if the idea of a game whose answer is known before you start appeals, the Towers of Hanoi does the same trick with arithmetic instead of binary.

Frequently asked questions

+ What is the winning strategy in Nim?

Write each row's count in binary, stack them up, and add each column without carrying. If every column comes out even, the player about to move is lost. If some column is odd, there is a winning move, and it is in a row that has a 1 in the leftmost odd column: reduce that row so every column becomes even again. Hand your opponent an all-even table every turn and they can never hand one back.

+ What is a nim-sum?

The exclusive-or of the row lengths — the same thing as adding the binary columns without carrying. A nim-sum of zero is exactly the set of positions that lose for whoever has to move. Charles Bouton proved this in 1901, and it is one of the shortest complete solutions any game has.

+ Does the player who goes first win at Nim?

It depends entirely on the rows, and the traditional 1-3-5-7 board is the awkward case: its nim-sum is already zero, so the player who goes first loses against correct play. Whoever taught you the game and generously let you start was, mathematically speaking, taking you for a ride.

+ What is misère Nim?

The version where taking the last mark loses instead of wins. It is the one played in the film Last Year at Marienbad, which is why it is sometimes called the Marienbad game. The strategy is almost identical — as long as some row still holds two or more marks, the nim-sum rule is unchanged. It only differs at the very end.

+ How different are the normal and misère games really?

Barely, and we counted it. Of the 2,400 positions with up to four rows of up to six marks, 2,385 behave identically in both versions and only 15 differ — and every one of those 15 consists of nothing but single marks. That is why almost everybody plays misère Nim correctly right up to the last few moves and then loses it.

+ Why is Nim important in mathematics?

Because of the Sprague–Grundy theorem, proved independently in 1935 and 1939: every impartial game — one where both players have the same moves available and there is no chance — is equivalent to a single heap of Nim of some size. Nim is not one solved game among many. It is the game all the others turn into once you look at them properly.

+ How hard is the computer here?

There are three, and they differ in what they know rather than how far they search. “Perfect” plays the full nim-sum and cannot be beaten from a position that was already lost for you. “Pairs only” knows the strategy most people actually know — make the rows into equal pairs and copy every move — which we measured at converting 51.5% of won positions in the normal game and 0.3% in misère, no better than moving at random. “Careless” takes something at random.

+ Where does the name come from?

Charles Bouton, who solved the game at Harvard in 1901, gave it the name. He suggested it comes from the German nimm, meaning take. The game itself is much older and was played in China and across Europe long before anybody wrote down why it works.

+ Can I play Nim against another person?

Yes. “Play a friend” opens a table with a short code — send it to anyone and they join from their own device, with a chat box included. Against a person the binary table starts closed, because anyone who can see it wins.

+ Is it free?

Yes, and there is no sign-up, for the online tables either. Your running score against the computer is kept in your own browser and nothing is sent anywhere.

More games like this

River Crossing — screenshot of the browser gameMath & Logic
Logic Puzzle1 Player

River Crossing

Four puzzles, three of them from the oldest surviving puzzle book. Getting everyone across is easy; doing it in the fewest crossings is the actual question.

  • All three of Alcuin's river problems from around 800, plus missionaries and cannibals
  • A light says whether you are still on a shortest route — computed, not guessed
  • The wolf and goat puzzle has exactly two solutions, and we show the whole state graph
Play River Crossing
Towers of Hanoi — screenshot of the browser gameMath & Logic
Recursion1 Player

Towers of Hanoi

The puzzle where the answer is known before you start: 2ⁿ−1 moves, never fewer. The question is whether you can find them — and the counter says the instant you cannot.

  • Three to ten discs, with the target 2ⁿ−1 always in view
  • A counter that tells you the moment you leave the shortest path
  • Watch it solve itself, slowly, and see the recursion
Play Towers of Hanoi
MENACE — screenshot of the browser gameMath & Logic
Machine Learning1 Player

MENACE

A machine built from 304 matchboxes that learns noughts and crosses by throwing away the beads for moves that lost. It starts knowing nothing. Play it and watch it change.

  • The 1961 original, rule for rule — beads, matchboxes and all
  • Watch the box it opens and the beads it gains and loses
  • Train it against a human, a random player or perfect play
Play MENACE
Dots and Boxes — screenshot of the browser gameMath & Logic
Pencil Game2 Players

Dots and Boxes

Draw a line, close a box, go again. The rules take twenty seconds and the strategy takes years — because the winning move is usually the one that gives boxes away.

  • Three grid sizes and three computer levels
  • We solved the small boards exactly — the numbers are on this page
  • Two players on one device, no account needed
Play Dots and Boxes