Lemma

Nim

A game of pebbles and heaps whose entire outcome is settled in advance by the binary arithmetic hidden in the pile sizes.

Nim is a mathematical game of strategy in which two players alternately remove objects from several heaps, with the only rule being that a player must take at least one object per turn and may take any number so long as they all come from a single heap.18 It is the most well-known example of an impartial game — a game in which both players have the same available moves at every position, so that the only distinction between them is who moves first — and it has been completely solved, meaning an exact optimal strategy is known for any starting configuration.1 The heaps are also called piles and the objects stones.18

In the standard, or normal, form of the game, the player who removes the last stone wins, and the player unable to move loses.15 A variant called misère Nim reverses this: the player forced to take the last stone loses instead.1 In combinatorial game theory, the normal convention — the win going to the last player to move — is the default, and misère play requires only a small modification to the winning strategy near the end of the game.1

The game requires no board and no special equipment: it can be played with pebbles, coins, matchsticks, or any collection of small tokens, or, lacking them, with marks made on paper and crossed off in turn.5 Because the state of play is fully captured by the number of stones remaining in each pile, both players always know everything about the current position, making Nim a game of perfect information.8

Strategy

The complete solution rests on the nim-sum, the bitwise operation that computer scientists call XOR.1 To compute the nim-sum of two non-negative integers, each is written as a sum of distinct powers of two; powers appearing in both are canceled, and the remaining powers are added.1 For example, 3 (as 2¹ + 2⁰) combined with 5 (as 2² + 2⁰) cancels the shared 2⁰, leaving 2¹ + 2² = 6.1 The operation is associative, commutative, has 0 as identity, and is self-inverse in that any value combined with itself yields zero.1 These properties make the nim-sum of several piles well-defined regardless of the order in which they are combined.1

The governing theorem, proved by C. Bouton, states that the player to move wins in normal Nim if and only if the nim-sum of all the pile sizes is not zero.1 When the nim-sum is nonzero, a winning move exists that reduces some single pile so that the nim-sum of the resulting position becomes zero; from a position whose nim-sum is already zero, every move leaves a nonzero nim-sum, so the player to move loses against perfect play.1 Bouton’s proof proceeds from the base case in which all piles are empty — a position whose nim-sum is zero and in which the player to move has already lost — and builds outward using the arithmetic properties of the operation.1

In the classic starting position of piles of 3, 4, and 5, the nim-sum is 2 — nonzero — so the first player wins, the winning first move being to reduce the pile of 3 to a single stone, after which the three piles nim-sum to zero.18 A worked example of such a game runs nine moves, with the first player, having secured the zero-sum position, able to answer every reply until taking the final stone.1

Simpler special cases make the same logic visible without the arithmetic. With two piles of equal size, the second player wins by always mirroring the opponent’s move in the other pile; with two unequal piles, the first player wins by first equalizing them and then mirroring.8 This pairing strategy extends to any even number of equal-sized piles, where the second player pairs the heaps and mirrors within each pair.8 With five stones there are seven distinct starting configurations, and the first player has a winning strategy in all of them.8

In misère Nim the optimal play is nearly identical to normal play, diverging only once the position is about to consist of piles of size one: the correct aim is to leave an odd number of single-stone piles, the reverse of the even count that normal play seeks.1 This adjustment is applied only when the player’s move would otherwise leave no pile of size two or larger; up to that point, the normal strategy is followed unchanged.1

Play and context

A frequently taught restricted version of the game allows each player to remove only one or two objects from a single pile per turn, with the taker of the last object winning; it is often demonstrated by starting from a single row of a chosen number of counters, such as thirteen.5 Because pencil and paper suffice, Nim has become a common demonstration game in mathematics education, used to introduce students to the idea that a simple game can conceal a rigorous, complete strategy.5 As a two-player game of perfect information, it guarantees that one of the two players always possesses a winning strategy once the starting position is fixed, though which player that is depends on the configuration and identifying the correct sequence of responses is not always straightforward.8

The theory of Nim extends beyond the game itself through the Sprague–Grundy theorem, which assigns to every position in any impartial game a value — a “nimber” — equivalent to a single Nim heap, so that Nim serves as the canonical model to which a wide class of impartial games can be reduced.1

Adaptations in computing

Nim’s simplicity and complete solvability made it an early subject for automated play. Its rules require only counting and the XOR operation, which map directly onto digital hardware, and the game has long been used as a compact illustration of game-solving algorithms.1 The word “nim” has since accumulated many unrelated uses, including as the name of a statically typed, compiled systems programming language whose designers took its name as shorthand for Nimrod.211

History as an early computer game

The game’s foundations were established in 1901 by Charles L. Bouton of Harvard University, who published “Nim, A Game with a Complete Mathematical Theory” in the Annals of Mathematics and gave the game its modern name.1217 Bouton coined the term “nim,” most likely from the imperative of the German verb nehmen (“to take”), the imperative being nimm; the choice has been linked to the years he spent in Leipzig, where he received his doctorate.13 The paper both introduced the winning theory based on binary addition modulo two and helped give rise to the field of combinatorial game theory.1217 Although Bouton fixed the name and the mathematics, variants of the game had been played for centuries, with examples recorded in Europe as early as the 16th century and origins sometimes traced to ancient China, where a comparable pebble game was known as jianshiji, or “pick up pebbles”.1516

Because its rules reduce to counting and the bitwise XOR operation, Nim proved well suited to mechanization, and it became one of the earliest games to be implemented on computing machinery.12 In 1940 the American physicist Edward Uhler Condon and an associate built the Nimatron, an automatic machine that played a perfect game of Nim; it was exhibited at the New York World’s Fair in 1940.14 In 1951 the game inspired the Nimrod computer, one of the first machines built specifically to play a game.1218 The Nimrod is frequently cited alongside OXO and Tennis for Two among the earliest computer games, none of which were sold to the public because they were too large or too costly to leave the laboratory.18

Sources

1Nim | Brilliant Math & Science Wiki

Educational overview of the combinatorial game Nim, including rules, winning strategies, and the nim-sum theorem.

brilliant.org · retrieved Jul 11, 2026
2The Nim programming language · GitHub

GitHub organization for the Nim programming language, a statically typed systems language emphasizing efficiency and expressiveness.

github.com · retrieved Jul 11, 2026
5NIM: A super simple game

YouTube video explaining the basic rules and gameplay of the Nim mathematical strategy game for beginners.

youtube.com · retrieved Jul 11, 2026
8The Game of Nim

PDF guide from Madison Math Circle explaining Nim rules, examples, and exercises for students to develop winning strategies.

wiki.math.wisc.edu · retrieved Jul 11, 2026
11nim name origin? - Nim forum

Nim is a shorthand for Nimrod who was the first king of mankind. Nim the language is the first programming language that can be called…

forum.nim-lang.org · retrieved Jul 11, 2026
12The Secret of Nim. The Game of Nim Hides a Beautiful…

Essay explaining Nim as a mathematical strategy game with winning techniques based on binary arithmetic and the Nim-sum.

medium.com · retrieved Jul 11, 2026
13[PDF] THE PREHISTORY OF NIM GAME - Gathering 4 Gardner

Academic paper tracing the historical origins and evolution of Nim game across centuries and cultures.

gathering4gardner.org · retrieved Jul 11, 2026
14Nim | Ancient Number Game | Britannica

Britannica encyclopedia entry defining Nim as an ancient game with complete mathematical theory for determining safe and unsafe positions.

britannica.com · retrieved Jul 11, 2026
15Nim | Asia Society

Educational lesson plan for teaching Nim (jianshiji) to students as a game teaching subtraction, strategy, and mathematical thinking.

asiasociety.org · retrieved Jul 11, 2026
16The Noble and Most Ancient Game of NIM | Game Theory 3 - Problem Solving

UCL mathematics video lecture analyzing Nim's winning strategy through position analysis and game theory problem-solving techniques.

youtube.com · retrieved Jul 11, 2026
17[PDF] Nim, Binary Numbers and Variations - tom rocks maths

Academic paper on Nim strategy using binary numbers and the Nim-sum to determine winning positions in multi-pile games.

tomrocksmaths.com · retrieved Jul 11, 2026
18A brief history of video games (Part I) - Safwat Saleem

# A brief history of video games (Part I) - Safwat Saleem ## TED-Ed 22700000 subscribers ### Description View full lesson: Video games are everywhere…

youtube.com · retrieved Jul 11, 2026

Lineage / Influences

Influenced

shortassigns every impartial-game position a nimber equivalent to a single Nim heap, making Nim the canonical model for impartial games
Written by Lemma, an encyclopedia of art and inspiration. Every claim above is tied to a source in the margin — follow them wherever they lead. Generated reference text; check the sources before relying on it.