Three piles of tokens on the table. Two players, alternating. On your turn you take at least one token — one, several, the whole pile — but all of them from a single pile. Whoever takes the last token wins. A six-year-old can hold the rules in her head. That is exactly the problem: the game feels like a coin-flip of greed, and our computer opponent beats nearly everyone who sits down, because it carries a formula inside. A formula from 1901.

A game nobody can date

Variants of nim have been played since ancient times — that is the honest shape of the claim. The pinned encyclopedia article puts the caution in writing: many believe the game began in China, because it closely resembles jiǎn-shízǐ, "picking up stones"; but the origin is uncertain, and the earliest European references to nim date from the beginning of the 16th century. Bouton himself, writing the theory, confessed he had "not been able to discover much concerning its history" — only that certain forms were being played at American colleges and fairs, sometimes under the name Fan-Tan, which is not the Chinese game of that name. So he proposed a new one: the title of his paper is the name the game kept. That is the origin story we can actually stand on — a game that travelled from room to room without a birth certificate, and got its name the year it got its theory. The named ancestors that circulate online have no primary source we could link, and they stay off this record, in the fog where the evidence leaves them.

1901: the game with a birthday

That year Charles L. Bouton of Harvard published "Nim, A Game with a Complete Mathematical Theory" — a title that promises what no puzzle essay dares to promise, and delivers. The theory fits in a paragraph. Write the size of each pile in binary and add the columns without carrying — the operation modern computing calls XOR, exclusive or. Bouton called the positions whose carryless sum is zero "safe combinations", and proved the two things that matter: from a safe position, every move you can make breaks it; from an unsafe one, there is always a move that restores it. Hand your opponent a zero and, provided you never slip, you have won. Try 3, 5 and 7 — 011, 101, 111 — whose carryless sum is 001: fixable. Take one token from the pile of 3 and the board reads 2, 5, 7 — 010, 101, 111 — which sums to 000. From there the game is arithmetic. A footnote in the same paper dates the mirror version — where taking the last token loses: described in October 1899 by Mr. Paul E. More, who had the winning rule but no proof. Bouton supplied the proof; the catalog ships the classic, last-token-wins game.

The theorem outgrows the game

In 1936 Roland Sprague and, independently in 1939, Patrick Grundy proved the result that carries both names: every impartial game — both players always have the same moves available — is mathematically a pile of nim in disguise. Nim stopped being a game and became the alphabet of a field, combinatorial game theory. And the theorem built machines. In 1940 Westinghouse displayed the Nimatron at the New York World's Fair: a ton of electromechanical relays playing nim with light bulbs. Fairgoers lost it roughly 90,000 times in 100,000 games — though the operators, who knew its dozen preset layouts, could beat it on demand, the honest footnote on any "unbeatable" machine of that era. In 1951 Ferranti's Nimrod — built to play nim, to show off computing — ran at the Festival of Britain with its calculations slowed for the crowd. Martin Gardner gave nim a Scientific American "Mathematical Games" column in 1958; a quiet game of it sits at the heart of the film Last Year at Marienbad (1961).

The theorem plays in our catalog

The catalog's Nim board: three piles of round tokens on the light board — five blue, four green and six red — with the pile of six red tokens selected under a cream highlight band spanning the board.

Which brings us to our own Nim — the confession this article was built to make: the opponent you play there runs Bouton's calculation, verbatim, every turn. Three piles, sizes drawn per difficulty — 3 to 5 tokens each on Easy, 6 to 12 on Expert — and every turn the machine adds them in binary without carrying. If the sum is not zero, it trims exactly the pile that puts the sum back at zero: the safe combination of 1901, delivered between two clicks. If the sum is already zero — if it is lost — it removes something at random: the note above the routine says its only hope is that you blunder. No hints, no undo, deliberately: the reply is instant, and undoing a move would rewind a turn mathematics has already scored. From a nonzero position it does not err; every human victory is one Bouton described — be dealt a zero, or wait for the random flailings of a machine that knows it has won. The merit here is not the workshop's; a theorem from 1901 fits in a few lines, and we simply let it play.

Play against the theorem

The rules one last time: three piles, take at least one token from a single pile per turn, last token wins. Nim is free in the browser. Play a few on Easy, write the piles down in binary between turns, and watch the zeros appear. If you lose, you will at least know the year you were beaten in.