Nim is a
mathematical
Mathematics is a field of study that discovers and organizes methods, Mathematical theory, theories and theorems that are developed and Mathematical proof, proved for the needs of empirical sciences and mathematics itself. There are many ar ...
combinatorial game in which two players take turns removing (or "nimming") objects from distinct heaps or piles. On each turn, a player must remove at least one object, and may remove any number of objects provided they all come from the same heap or pile. Depending on the version being played, the goal of the game is either to avoid taking the last object or to take the last object.
Nim is fundamental to the
Sprague–Grundy theorem
In combinatorial game theory, the Sprague–Grundy theorem states that every impartial game under the normal play convention is equivalent to a one-heap game of nim, or to an infinite generalization of nim. It can therefore be represented ...
, which essentially says that every
impartial game
In combinatorial game theory, an impartial game is a game in which the allowable moves depend only on the position and not on which of the two players is currently moving, and where the payoffs are symmetric. In other words, the only difference be ...
is equivalent to a nim game with a single pile.
History
Variants of nim have been played since ancient times. The game is said to have originated in
China
China, officially the People's Republic of China (PRC), is a country in East Asia. With population of China, a population exceeding 1.4 billion, it is the list of countries by population (United Nations), second-most populous country after ...
—it closely resembles the Chinese game of (), or "picking stones"—but the origin is uncertain; the earliest European references to nim are from the beginning of the 16th century. Its current name was coined by
Charles L. Bouton of
Harvard University
Harvard University is a Private university, private Ivy League research university in Cambridge, Massachusetts, United States. Founded in 1636 and named for its first benefactor, the History of the Puritans in North America, Puritan clergyma ...
, who also developed the complete theory of the game in 1901, but the origins of the name were never fully explained. The ''
Oxford English Dictionary
The ''Oxford English Dictionary'' (''OED'') is the principal historical dictionary of the English language, published by Oxford University Press (OUP), a University of Oxford publishing house. The dictionary, which published its first editio ...
'' derives the name from the German verb , meaning "take".
At the
1939 New York World's Fair
The 1939 New York World's Fair (also known as the 1939–1940 New York World's Fair) was an world's fair, international exposition at Flushing Meadows–Corona Park in Queens, New York City, New York, United States. The fair included exhibitio ...
,
Westinghouse displayed a machine, the
Nimatron, that played nim. From May 11 to October 27, 1940, only a few people were able to beat the machine in that six-month period; if they did, they were presented with a coin that said "Nim Champ". It was also one of the first-ever electronic computerized games.
Ferranti
Ferranti International PLC or simply Ferranti was a UK-based electrical engineering and equipment firm that operated for over a century, from 1885 until its bankruptcy in 1993. At its peak, Ferranti was a significant player in power grid system ...
built a
nim-playing computer which was displayed at the
Festival of Britain in 1951. In 1952, Herbert Koppel, Eugene Grant and Howard Baller, engineers from the W. L. Maxson Corporation, developed a machine weighing which played nim against a human opponent and regularly won. A nim playing machine has been described made from
tinkertoys.
The game of nim was the subject of
Martin Gardner
Martin Gardner (October 21, 1914May 22, 2010) was an American popular mathematics and popular science writer with interests also encompassing magic, scientific skepticism, micromagic, philosophy, religion, and literatureespecially the writin ...
's February 1958
Mathematical Games column in ''
Scientific American
''Scientific American'', informally abbreviated ''SciAm'' or sometimes ''SA'', is an American popular science magazine. Many scientists, including Albert Einstein and Nikola Tesla, have contributed articles to it, with more than 150 Nobel Pri ...
''. A version of nim is played—and has symbolic importance—in the
French New Wave
The New Wave (, ), also called the French New Wave, is a French European art cinema, art film movement that emerged in the late 1950s. The movement was characterized by its rejection of traditional filmmaking conventions in favor of experimentat ...
film ''
Last Year at Marienbad'' (1961).
Game play and illustration
Nim is typically played as a
misère game, in which the player to take the last object loses. Nim can also be played as a "normal play" game whereby the player taking the last object wins. In either normal play or a misère game, when there is exactly one heap with at least two objects, the player who takes next can easily win. If this removes either all or all but one objects from the heap that has two or more, then no heaps will have more than one object, so the players are forced to alternate removing exactly one object until the game ends. If the player leaves an even number of non-zero heaps (as the player would do in normal play), the player takes last; if the player leaves an odd number of heaps (as the player would do in misère play), then the other player takes last.
The normal game is between two players and is played with three heaps of any number of objects. The two players alternate taking any number of objects from any one of the heaps. The goal is to be the last to take an object. In misère play, the goal is instead to ensure that the opponent is forced to take the last remaining object.
The following example of a normal game is played between fictional players
Bob and Alice, who start with heaps of three, four and five objects.
Winning positions
The practical strategy to win at the game of nim is for a player to get the other into one of the following positions, and every successive turn afterwards they should be able to make one of the smaller positions. Only the last move changes between misère and normal play.
For the generalisations, ''n'' and ''m'' can be any value > 0, and they may be the same.
Mathematical theory
Normal-play nim (or more precisely the system of
nimber
In mathematics, the nimbers, also called Grundy numbers (not to be confused with Grundy chromatic numbers), are introduced in combinatorial game theory, where they are defined as the values of heaps in the game Nim. The nimbers are the ordin ...
s) is fundamental to the
Sprague–Grundy theorem
In combinatorial game theory, the Sprague–Grundy theorem states that every impartial game under the normal play convention is equivalent to a one-heap game of nim, or to an infinite generalization of nim. It can therefore be represented ...
, which essentially says that in normal play every
impartial game
In combinatorial game theory, an impartial game is a game in which the allowable moves depend only on the position and not on which of the two players is currently moving, and where the payoffs are symmetric. In other words, the only difference be ...
is equivalent to a nim heap that yields the same outcome when played in parallel with other normal play impartial games (see
disjunctive sum).
While all normal-play impartial games can be assigned a nim value, that is not the case under the misère convention. Only
tame games can be played using the same strategy as misère nim.
Nim is a special case of a
poset game where the
poset
In mathematics, especially order theory, a partial order on a Set (mathematics), set is an arrangement such that, for certain pairs of elements, one precedes the other. The word ''partial'' is used to indicate that not every pair of elements need ...
consists of disjoint
chains (the heaps).
The evolution graph of the game of nim with three heaps is the same as three branches of the evolution graph of the
Ulam–Warburton automaton.
Nim has been mathematically
solved for any number of initial heaps and objects, and there is an easily calculated way to determine which player will win and which winning moves are open to that player.
The key to the theory of the game is the
binary digital sum of the heap sizes, i.e., the sum (in binary), neglecting all carries from one digit to another. This operation is also known as "
bitwise xor
In computer programming, a bitwise operation operates on a bit string, a bit array or a binary numeral (considered as a bit string) at the level of its individual bits. It is a fast and simple action, basic to the higher-level arithmetic opera ...
" or "vector addition over
GF(2)" (bitwise addition modulo 2). Within
combinatorial game theory
Combinatorial game theory is a branch of mathematics and theoretical computer science that typically studies sequential games with perfect information. Research in this field has primarily focused on two-player games in which a ''position'' ev ...
it is usually called the nim-sum, as it will be called here. The nim-sum of ''x'' and ''y'' is written to distinguish it from the ordinary sum, . An example of the calculation with heaps of size 3, 4, and 5 is as follows:
Binary Decimal
The decimal numeral system (also called the base-ten positional numeral system and denary or decanary) is the standard system for denoting integer and non-integer numbers. It is the extension to non-integer numbers (''decimal fractions'') of th ...
011
2 3
10 Heap A
100
2 4
10 Heap B
101
2 5
10 Heap C
---
010
2 2
10 The nim-sum of heaps A, B, and C, 3 ⊕ 4 ⊕ 5 = 2
An equivalent procedure, which is often easier to perform mentally, is to express the heap sizes as sums of distinct
powers of 2, cancel pairs of equal powers, and then add what is left:
3 = 0 + 2 + 1 = 2 1 Heap A
4 = 4 + 0 + 0 = 4 Heap B
5 = 4 + 0 + 1 = 4 1 Heap C
--------------------------------------------------------------------
2 = 2 What is left after canceling 1s and 4s
In normal play, the winning strategy is to finish every move with a nim-sum of 0. This is always possible if the nim-sum is not zero before the move. If the nim-sum is zero, then the next player will lose if the other player does not make a mistake. To find out which move to make, let X be the nim-sum of all the heap sizes. Find a heap where the nim-sum of X and heap-size is less than the heap-size; the winning strategy is to play in such a heap, reducing that heap to the nim-sum of its original size with X. In the example above, taking the nim-sum of the sizes is . The nim-sums of the heap sizes A=3, B=4, and C=5 with X=2 are
: ''A'' ⊕ ''X'' = 3 ⊕ 2 = 1
ince (011) ⊕ (010) = 001 : ''B'' ⊕ ''X'' = 4 ⊕ 2 = 6
: ''C'' ⊕ ''X'' = 5 ⊕ 2 = 7
The only heap that is reduced is heap A, so the winning move is to reduce the size of heap A to 1 (by removing two objects).
As a particular simple case, if there are only two heaps left, the strategy is to reduce the number of objects in the bigger heap to make the heaps equal. After that, no matter what move the opponent makes, the player can make the same move on the other heap, guaranteeing that they take the last object.
When played as a misère game, nim strategy is different only when the normal play move would leave only heaps of size one. In that case, the correct move is to leave an odd number of heaps of size one (in normal play, the correct move would be to leave an even number of such heaps).
These strategies for normal play and a misère game are the same until the number of heaps with at least two objects is exactly equal to one. At that point, the next player removes either all objects (or all but one) from the heap that has two or more, so no heaps will have more than one object (in other words, so all remaining heaps have exactly one object each), so the players are forced to alternate removing exactly one object until the game ends. In normal play, the player leaves an even number of non-zero heaps, so the same player takes last; in misère play, the player leaves an odd number of non-zero heaps, so the other player takes last.
In a misère game with heaps of sizes three, four and five, the strategy would be applied like this:
Proof of the winning formula
The soundness of the optimal strategy described above was demonstrated by C. Bouton.
Theorem. In a normal nim game, the player making the first move has a winning strategy if and only if the nim-sum of the sizes of the heaps is not zero. Otherwise, the second player has a winning strategy.
''Proof:'' Notice that the nim-sum (⊕) obeys the usual
associative
In mathematics, the associative property is a property of some binary operations that rearranging the parentheses in an expression will not change the result. In propositional logic, associativity is a valid rule of replacement for express ...
and
commutative
In mathematics, a binary operation is commutative if changing the order of the operands does not change the result. It is a fundamental property of many binary operations, and many mathematical proofs depend on it. Perhaps most familiar as a pr ...
laws of addition (+) and also satisfies an additional property, ''x'' ⊕ ''x'' = 0.
Let be the sizes of the heaps before a move, and ''y''
1, ..., ''y
n'' the corresponding sizes after a move. Let ''s'' = ''x''
1 ⊕ ... ⊕ ''x
n'' and ''t'' = ''y''
1 ⊕ ... ⊕ ''y
n''. If the move was in heap ''k'', we have ''x
i'' = ''y
i'' for all , and ''x
k'' > ''y
k''. By the properties of ⊕ mentioned above, we have
That is, to update the total nim sum
after updating the
heap, we need to cancel it from
by nim summing with
, and then nim sum in
.
The theorem follows by induction on the length of the game from these two lemmas.
Lemma 1. If ''s'' = 0, then ''t'' ≠ 0 no matter what move is made.
''Proof:'' If there is no possible move, then the lemma is
vacuously true (and the first player loses the normal play game by definition). Otherwise, any move in heap ''k'' will produce ''t'' = ''x
k'' ⊕ ''y
k'' from (*). This number is nonzero, since ''x
k'' ≠ ''y
k''.
Lemma 2. If ''s'' ≠ 0, it is possible to make a move so that ''t'' = 0.
''Proof:'' Let ''d'' be the position of the leftmost (most significant) nonzero bit in the binary representation of ''s'', and choose ''k'' such that the ''d''th bit of ''x
k'' is also nonzero. (Such a ''k'' must exist, since otherwise the ''d''th bit of ''s'' would be 0.)
Then letting ''y
k'' = ''s'' ⊕ ''x
k'', we claim that ''y
k'' < ''x
k'': all bits to the left of ''d'' are the same in ''x
k'' and ''y
k'', bit ''d'' decreases from 1 to 0 (decreasing the value by 2
''d''), and any change in the remaining bits will amount to at most 2
''d''−1. The first player can thus make a move by taking ''x
k'' − ''y
k'' objects from heap ''k'', then
''t'' = ''s'' ⊕ ''x
k'' ⊕ ''y
k'' (by (*))
= ''s'' ⊕ ''x
k'' ⊕ (''s'' ⊕ ''x
k'')
= 0.
The modification for misère play is demonstrated by noting that the modification first arises in a position that has only one heap of size 2 or more. Notice that in such a position ''s'' ≠ 0, and therefore this situation has to arise when it is the turn of the player following the winning strategy. The normal play strategy is for the player to reduce this to size 0 or 1, leaving an even number of heaps with size 1, and the misère strategy is to do the opposite. From that point on, all moves are forced.
Variations
The subtraction game
File:Subtraction_game_SMIL.svg, Interactive subtraction game: Players take turns removing 1, 2 or 3 objects from an initial pool of 21 objects. The player taking the last object wins.
defaul
In another game which is commonly known as nim (but is better called the
subtraction game), an upper bound is imposed on the number of objects that can be removed in a turn. Instead of removing arbitrarily many objects, a player can only remove 1 or 2 or ... or ''k'' at a time. This game is commonly played in practice with only one heap.
Bouton's analysis carries over easily to the general multiple-heap version of this game. The only difference is that as a first step, before computing the nim-sums we must reduce the sizes of the heaps
modulo
In computing and mathematics, the modulo operation returns the remainder or signed remainder of a division, after one number is divided by another, the latter being called the '' modulus'' of the operation.
Given two positive numbers and , mo ...
''k'' + 1. If this makes all the heaps of size zero (in misère play), the winning move is to take ''k'' objects from one of the heaps. In particular, in ideal play from a single heap of ''n'' objects, the second player can win
if and only if
In logic and related fields such as mathematics and philosophy, "if and only if" (often shortened as "iff") is paraphrased by the biconditional, a logical connective between statements. The biconditional is true in two cases, where either bo ...
* ''0'' = n (mod ''k'' + 1) (in normal play), or
* ''1'' = n (mod ''k'' + 1) (in misère play).
This follows from calculating the
nim-sequence of ''S''(1, 2, ..., ''k''),
from which the strategy above follows by the
Sprague–Grundy theorem
In combinatorial game theory, the Sprague–Grundy theorem states that every impartial game under the normal play convention is equivalent to a one-heap game of nim, or to an infinite generalization of nim. It can therefore be represented ...
.
The 21 game
The game "21" is played as a misère game with any number of players who take turns saying a number. The first player says "1" and each player in turn increases the number by 1, 2, or 3, but may not exceed 21; the player forced to say "21" loses. This can be modeled as a subtraction game with a heap of objects. The winning strategy for the two-player version of this game is to always say a multiple of 4; it is then guaranteed that the other player will ultimately have to say 21; so in the standard version, wherein the first player opens with "1", they start with a losing move.
The 21 game can also be played with different numbers, e.g., "Add at most 5; lose on 34".
A sample game of 21 in which the second player follows the winning strategy:
The 100 game
A similar version is the "100 game": Two players start from 0 and alternately add a number from 1 to 10 to the sum. The player who reaches 100 wins. The winning strategy is to reach a number in which the digits are subsequent (e.g., 01, 12, 23, 34,...) and control the game by jumping through all the numbers of this sequence. Once a player reaches 89, the opponent can only choose numbers from 90 to 99, and the next answer can in any case be 100.
A multiple-heap rule
In another variation of nim, besides removing any number of objects from a single heap, one is permitted to remove the same number of objects from each heap.
Circular nim
Yet another variation of nim is "circular nim", wherein any number of objects are placed in a circle and two players alternately remove one, two or three adjacent objects. For example, starting with a circle of ten objects,
. . . . . . . . . .
three objects are taken in the first move
_ . . . . . . . _ _
then another three
_ . _ _ _ . . . _ _
then one
_ . _ _ _ . . _ _ _
but then three objects cannot be taken out in one move.
Grundy's game
In
Grundy's game, another variation of nim, a number of objects are placed in an initial heap and two players alternately divide a heap into two nonempty heaps of different sizes. Thus, six objects may be divided into piles of 5+1 or 4+2, but not 3+3. Grundy's game can be played as either misère or normal play.
Greedy nim
Greedy nim is a variation wherein the players are restricted to choosing stones from only the largest pile.
[
; ; ; ;
] It is a finite
impartial game
In combinatorial game theory, an impartial game is a game in which the allowable moves depend only on the position and not on which of the two players is currently moving, and where the payoffs are symmetric. In other words, the only difference be ...
. Greedy nim misère has the same rules as greedy nim, but the last player able to make a move loses.
Let the largest number of stones in a pile be ''m'' and the second largest number of stones in a pile be ''n''. Let ''p''
''m'' be the number of piles having ''m'' stones and ''p''
''n'' be the number of piles having ''n'' stones. Then there is a theorem that game positions with ''p''
''m'' even are ''P'' positions.
[
] This theorem can be shown by considering the positions where ''p''
''m'' is odd. If ''p''
''m'' is larger than 1, all stones may be removed from this pile to reduce ''p''
''m'' by 1 and the new ''p''
''m'' will be even. If ''p''
''m'' = 1 (i.e. the largest heap is unique), there are two cases:
* If ''p''
''n'' is odd, the size of the largest heap is reduced to ''n'' (so now the new ''p''
''m'' is even).
* If ''p''
''n'' is even, the largest heap is removed entirely, leaving an even number of largest heaps.
Thus, there exists a move to a state where ''p''
''m'' is even. Conversely, if ''p''
''m'' is even, if any move is possible (''p''
''m'' ≠ 0), then it must take the game to a state where ''p''
''m'' is odd. The final position of the game is even (''p''
''m'' = 0). Hence, each position of the game with ''p''
''m'' even must be a ''P'' position.
Index-''k'' nim
A generalization of multi-heap nim was called "nim
" or "index-''k''" nim by
E. H. Moore, who analyzed it in 1910. In index-''k'' nim, instead of removing objects from only one heap, players can remove objects from at least one but up to ''k'' different heaps. The number of elements that may be removed from each heap may be either arbitrary or limited to at most ''r'' elements, like in the "subtraction game" above.
The winning strategy is as follows: Like in ordinary multi-heap nim, one considers the binary representation of the heap sizes (or heap sizes modulo ''r'' + 1). In ordinary nim one forms the XOR-sum (or sum modulo 2) of each binary digit, and the winning strategy is to make each XOR sum zero. In the generalization to index-''k'' nim, one forms the sum of each binary digit modulo ''k'' + 1.
Again, the winning strategy is to move such that this sum is zero for every digit. Indeed, the value thus computed is zero for the final position, and given a configuration of heaps for which this value is zero, any change of at most ''k'' heaps will make the value non-zero. Conversely, given a configuration with non-zero value, one can always take from at most ''k'' heaps, carefully chosen, so that the value will become zero.
Building nim
Building nim is a variant of nim wherein the two players first construct the game of nim. Given ''n'' stones and ''s'' empty piles, the players, alternating turns, place exactly one stone into a pile of their choice. Once all the stones are placed, a game of Nim begins, starting with the next player that would move. This game is denoted ''BN(n,s)''.
Higher-dimensional nim
''n''-d nim is played on a
board, whereon any number of continuous pieces can be removed from any hyper-row. The starting position is usually the full board, but other options are allowed.
Graph nim
The starting board is a disconnected graph, and players take turns to remove adjacent vertices.
Candy nim
Candy nim is a version of normal-play nim in which players try to achieve two goals at the same time: taking the last object (in this case, candy) and taking the maximum number of candies by the end of the game.
See also
* ''
Android Nim''
*
Chomp
*
Dr. NIM
*
Fibonacci nim
*
Fuzzy game
*
Hackenbush
*
Notakto
*
Octal games
*
Raymond Redheffer
*
Scarney
*
Star (game theory)
In combinatorial game theory, star, written as or , is the value given to the game where both players have only the option of moving to the zero game. Star may also be denoted as the surreal form . This game is an unconditional first-player win.
...
*
Subtract a square
*
Tri-nim
*
Turing Tumble
*
Zero game
References
Further reading
* W. W. Rouse Ball: ''Mathematical Recreations and Essays'', The Macmillan Company, 1947.
* John D. Beasley: ''The Mathematics of Games'', Oxford University Press, 1989.
* Elwyn R. Berlekamp, John H. Conway, and Richard K. Guy: ''
Winning Ways for your Mathematical Plays'', Academic Press, Inc., 1982.
*
Manfred Eigen and
Ruthild Winkler: ''Laws of the Game'', Princeton University Press, 1981.
* Walter R. Fuchs: ''Computers: Information Theory and Cybernetics'', Rupert Hart-Davis Educational Publications, 1971.
* G. H. Hardy and E. M. Wright: ''An Introduction to the Theory of Numbers'', Oxford University Press, 1979.
* Edward Kasner and James Newman: ''
Mathematics and the Imagination'', Simon and Schuster, 1940.
* M. Kaitchik: ''Mathematical Recreations'', W. W. Norton, 1942.
* Donald D. Spencer: ''Game Playing with Computers'', Hayden Book Company, Inc., 1968.
External links
*
50-pound computer plays Nim – ''
The New Yorker
''The New Yorker'' is an American magazine featuring journalism, commentary, criticism, essays, fiction, satire, cartoons, and poetry. It was founded on February 21, 1925, by Harold Ross and his wife Jane Grant, a reporter for ''The New York T ...
'' - "Talk of the Town" August, 1952 {{subscription required
The hot game of Nim– Nim theory and connections with other games at
cut-the-knot
Alexander Bogomolny (January 4, 1948 July 7, 2018) was a Soviet Union, Soviet-born Israeli Americans, Israeli-American mathematician. He was Professor Emeritus of Mathematics at the University of Iowa, and formerly research fellow at the Moscow ...
Nimand 2-dimensiona
SuperNimat
cut-the-knot
Alexander Bogomolny (January 4, 1948 July 7, 2018) was a Soviet Union, Soviet-born Israeli Americans, Israeli-American mathematician. He was Professor Emeritus of Mathematics at the University of Iowa, and formerly research fellow at the Moscow ...
Mathematical games
Combinatorial game theory
Articles containing proofs
Articles with example Python (programming language) code
Solved games
cs:Rozšířená forma#NIM