Nim

Take the last counter and win. The trick is not counting.

Loading…

The Sprague–Grundy lens

Moves

“Count how many are left” — tested

“Nim is about taking the last object, so the winning idea is to count how many are left.”

That is the sentence this app was built to check. The verdict is that it is false as a general rule and exactly right on one describable set — and the set explains why the sentence is so tempting.

Counting as a prediction

Take the claim at its word: guess that the player to move has lost whenever an even number of counters is left. Over every Nim position in two enumerated spaces ( positions of four heaps of 0–7 and of six heaps of 0–4, in all) that guess is right times and wrong times — % accurate, 95% CI –%. Its smallest counterexample is a single heap of two: two counters left, and the player to move wins by taking both.

Where it is exactly right, and why

On the positions whose every heap holds 0 or 1 counter, counting is right every single time. That is not luck. When every heap is a single counter the nim‑sum is the parity of the count: the binary columns collapse to one column, and XOR of a column of ones is exactly “odd or even”. The counting rule is the real rule with all but one bit thrown away. Everywhere else the discarded bits decide the game.

Counting as a strategy

Sharper test: hand each strategy a position that is already won and make it play out against perfect defence. A perfect player converts every one ( of ). A player who always moves to leave an even total converts of — %, 95% CI –%. Random play converts of (%). The counting player really is better than chance — two‑proportion z = — and it still throws away about games in 100 that it had already won.

Misere: where the rule changes, exactly

Play the same game with taking the last counter a loss and almost nothing changes. Two statements, both checked against brute force on all positions:

“Only at the very end” is right, but it is not rare. Over misere games played out perfectly from random openings, every single one passed through a position where the two strategies part company. The mean game lasts moves and the first divergence arrives at move ; % of all moves played (95% CI –%) are ones where knowing the misere rule matters. A player who knows only the normal-play rule does not lose misere Nim occasionally; they lose the endgame of every game they should have won.

What was verified, and how

The engine is checked by assertions. The Grundy values are re-derived a second time by a brute-force search that knows only the move rules and who moved last — no XOR, no mex — recovering each position's value as the one Nim heap size that makes the sum a loss for the mover ( positions, plus checks that no other heap size works). Five deliberately broken rules were each proven to break the match before anything they guard was believed; a sixth — a mex that starts counting at 1 — turned out to change nothing at all on Kayles, because no row of pins is ever a loss for the player to move, so it is recorded as inert rather than counted as a pass.