Nim Calculator

Set the heaps to match your game and this solver will tell you — with certainty, not estimation — whether you are winning, list every move that wins, and show you the binary working behind the answer. Handles normal play and misère.

Built by Brian Hamilton

Which version are you playing?
Set your heaps

The one calculator here that is never guessing

Every other solver on this site searches ahead and scores what it finds. It gives you a good move and tells you how deep it looked, because it cannot see to the end of the game.

Nim is different. It was solved completely by Charles Bouton in 1901, and the answer is arithmetic rather than search. Any position can be settled exactly, instantly, and with no uncertainty at all — so this page does not report a depth, an evaluation or a best guess. It reports whether you are winning, and if you are, it lists every move that keeps it that way.

Add the heaps in binary and see what is left over

Write each heap size in binary, stack them up, and add each column without carrying — that is, count whether the number of ones in that column is odd or even. The result is called the Nim-sum. Here is the standard opening position:

Heaps of 1, 3, 5 and 7 written in binary. Every column has an even number of ones.
Heap421
1001
3011
5101
7111
Ones in the column224
Nim-sum000

Every column comes out even, so the Nim-sum is zero and the heaps cancel exactly. That is the whole theorem: a Nim-sum of zero means the player about to move loses, and any other value means they can win.

The standard opening position of 1, 3, 5 and 7 objects The Nim-sum of these four heaps is zero, so the position is balanced and there is no winning move. Whoever has to move first loses against perfect play.
The familiar 1–3–5–7 opening. It looks like a fair start and it is not: the Nim-sum is zero, so whoever moves first loses against a player who knows this. The calculator reports no winning moves at all from here.

That is worth sitting with. The most commonly used Nim setup is a loss for the player who goes first. If you have been losing at Nim and could not work out what you were doing wrong, the answer may be that you were doing nothing wrong.

Finding the move

When the Nim-sum is not zero, you can always fix it. Take the Nim-sum, exclusive-or it with each heap in turn, and look for a heap where the answer is smaller than the heap itself. Cut that heap down to the answer.

With heaps of 1, 3 and 5 the Nim-sum is 7. Checking each heap: 1 gives 6, which is larger; 3 gives 4, larger again; but 5 gives 2, which is smaller. So the winning move is to reduce the heap of five down to two — taking three objects.

Heaps of 1, 3 and 5 with the only winning move marked The Nim-sum of 1, 3 and 5 is seven, so the position is unbalanced. Removing three objects from the heap of five leaves heaps of 1, 3 and 2, whose Nim-sum is zero.
Heaps of 1, 3 and 5. The three dashed objects are the ones to remove. That leaves 1, 3 and 2, whose Nim-sum is zero — and the calculator confirms this is the only winning move in the position.

Sometimes there are several. From heaps of 3, 5 and 7 the Nim-sum is 1, and taking a single object from any of the three heaps balances it. The calculator lists all of them rather than picking one, because knowing you have three safe options is more useful than being handed one.

Where the rule breaks: misère play

Change the goal so that taking the last object loses, and the theorem needs one amendment. It is not a different strategy for the whole game — only for the end of it.

  • While any heap holds two or more objects, play exactly as before. Balance the Nim-sum and hand the problem over.
  • Once every heap holds a single object, ignore the Nim-sum entirely and count the heaps. You want your opponent to face an odd number, because then they must take the last one.
The same position, analysed both ways. The answers differ by a single object.
HeapsGoalWinning moveLeaves
1, 1, 1, 3Last object winsTake 2 from the heap of 31, 1, 1, 1 — an even number of single heaps
Last object losesTake 3 from the heap of 31, 1, 1 — an odd number of single heaps

One object of difference, and it decides the game. This is the point at which most people playing the “last one loses” version go wrong, because the normal-play habit is to clear the big heap completely. Switch the toggle at the top of the page and the calculator will apply whichever rule you are actually playing under.

What the calculator shows you

  • A verdict — winning or losing with perfect play. Not a score.
  • The binary table, with the odd columns marked, so you can see where the imbalance is rather than taking the answer on trust.
  • Every winning move, with what each one leaves behind.
  • An explanation in words of why the position stands as it does, including which rule is being applied when misère changes the answer.

The one honest limitation

If a position is lost, it is genuinely lost — there is no clever move to find, and the calculator will say so rather than offering you the least bad option. Your only hope is that your opponent errs. Every position is either won or lost; there are no draws in Nim and no positions where the answer is unclear.

The heap sizes here are capped at 30 objects across up to 8 heaps, which is far beyond any normal game. The mathematics does not care about the size — the cap is only there to keep the display readable.

Names and variants

Nim is also played as Marienbad, after the 1961 film Last Year at Marienbad in which a character wins repeatedly with the 1–3–5–7 position, and as the matchstick game 21 or Nim-21 when played with a single heap and a limit on how many you may take. Single-heap Nim with a take-limit is a different, simpler game and is not covered by this calculator.

The version here is the classic one: any number of heaps, take as many as you like from exactly one heap per turn, and either goal for the last object.

Play a full game

To play rather than analyse, the free Nim game offers several starting positions, both variants, and an unbeatable opponent that uses this same arithmetic. There are matching analysers for Checkers, Reversi, Nine Men’s Morris, 4 in a Row and Gomoku — though those, unlike this one, are searching rather than proving. You may also like Peg Solitaire and Tic-Tac-Toe, or the rest of the free online puzzle games.