Recall that a game is called impartial if, from any subposition of , both Left and Right have the same options. In other words, the left options of are the same as the right options of , and all options of are impartial. In particular, , since switching the roles of Left and Right does not change the game.
Since the left and right options of are the same, it is a bit wasteful to write out both left and write options. Hence we will typically just write the set of options once, so we will abbreviate the impartial game
as
and we will use to refer to a typical option of .
In Nim, each player picks a pile of stones and removes at least one stone from that pile. Hence from a pile of stones, either player can move to a pile of , , , …, or stones. We denote the Nim game consisting of a single pile of stones by .
Henceand so forth.
Any Nim position is therefore just a sum of nimbers (corresponding to the sizes of the piles).
It might seem as if there should be many impartial games that are not nimbers: for instance, a game like is not isomorphic to any nimber. However, the following theorem shows that in fact any impartial game is equal to some nimber.
If , then we call the nim value of and write . Note that is a second player win (that is, lies in ) if and only if .
To prove the Sprague-Grundy Theorem, we will need to determine the nim value of games like . This is given by the following proposition, called the Mex Rule.
Definition 6.3 For any finite set of nonnegative integers , the minimum excluded value or mex of , denoted , is the smallest nonnegative integer not lying in .
Proof of Mex Rule. We need to show that is a win for the second player. For any nonnegative integer , is both an option of and .It is an option of by definition of , and it is an option of because is smaller than the minimum excluded value .Hence if the first player moves to in either component, the second player can move to in the other component, leaving , so the second player wins.
Otherwise, the first player must move to some option of with .Recall that is not an option of .Then the second player can move in to , leaving , so the second player wins.
(In our earlier parlance, each option of with is reversible through .)
It is now straightforward to prove the Sprague-Grundy Theorem.
Proof of Sprague-Grundy Theorem. Let be an impartial game. By induction, each equals some . Thus , where .
The Sprague-Grundy Theorem and Mex Rule can be summarized by the following equation:
In theory, one can use the Mex Rule to find the nim value of any impartial game. For instance, let’s use it to compute the sum of two nimbers. So as to avoid having to write ’s everywhere, we will define to be the nim value of , or in other words, . The operation is called nim addition. For instance, for all since .
By definition,
In other words,
We can then create a nim addition table by noting that each entry must be the mex of the entries above it in the same column ( for ) and to the left of it in the same row ( for ). This allows us to fill in entries moving outward from the upper left corner.
For instance, suppose that we have filled the table up to this point:
To find , we must compute the mex of all numbers above it in its column and to its left in its row. Hence . The addition table will end up looking something like the following.
You may be able to spot some patterns in this table, which suggests that one may be able to give an explicit formula for without having to work through this whole process. Indeed, this is given by the following theorem.
Theorem 6.6 For any nonnegative integer , let be the binary representation of , so that with for all . Similarly let have binary representation .
Then has binary representation , where if and are the same, and if and are different.
Example 6.7 Let us compute . Since , the binary representation of is . Likewise, , so the binary representation of is . We then “add without carrying”:
It follows that , or equivalently, . Note that in the resulting table, each column has an even number of ’s.
Proof of theorem. Let be the number with binary representation . Note that by construction, is even for all . We will show that this condition is equivalent to (that is, that is a second player win) by induction.
Suppose (without loss of generality) that Alice moves from to for some . Let be the leftmost position in which the th bit of and differ. Since , we must have and . Since is even, either or must be ; say it is . Then let be the number such that is even for all . Then and all bits to the left are unchanged from , so . Hence Bob can win by moving from to , which equals by induction.
Example 6.8 Suppose that in the game , which we claim is a second player win, Alice takes six stones from , leaving .
Since the leftmost bit in which and differ is in the place (where the digit changed from a to a ), that column now has an odd number of ’s. In particular, one of the other numbers must have a in that column, in this case, . Hence we can replace with a smaller number to again guarantee that each column has an even number of ’s. Here, Bob’s winning move is to take two stones , leaving .
It is just as easy to find the winning move in any Nim position by adding together the values of all the piles using nim addition. When nim-adding several numbers, simply list all of them in binary. A bit of the nim sum will be if there are an odd number of other ’s in that column, and otherwise. If the nim sum is already , then there is no winning move. Otherwise, find the leftmost bit in the nim sum that is . There must be some pile that also has that bit equal to , so take the appropriate number of stones from that pile to make the total nim sum . The size of the new pile can be found by nim-adding the old nim sum to the size of the pile to be changed.
Many impartial games fall into the category of taking and breaking games, which are games that involve piles of stones of various sizes in which a move consists of taking some number of stones from a pile and then possibly breaking the remaining stones into smaller piles according to some rules. For such games, one can compute the nim values of a single pile recursively using the Mex Rule.
When our choice of game is implicit, we will write for the nim value of a pile of size in that game. We will call the sequence
the nim sequence of the game.
The simplest such games are the subtraction games Subtraction() for any subset of positive integers. These games are played exactly like Nim except that one is only allowed to remove a number of stones equal to a value in on a single turn. Then the Mex Rule immediately gives that
Example 6.10 In Subtraction(1,2,3), one is only allowed to remove , , or stones from any pile. We can compute by noting that each must be the mex of the three previous values, giving the following nim sequence.
A simple induction shows that the nim sequence is periodic with period 4.
To determine the winning move in a position consisting of several piles, simply imagine that each pile is instead a pile in a standard game of Nim according to its nim value. Then for any winning move in that Nim game, you can find a move in the original game that changes the nim values as desired.
Example 6.11 Suppose that in Subtraction(1,2,3), one receives a position with piles of sizes , , and . Using the periodicity found above, we can find that the nim value of this position is
Since the total nim value is nonzero, this game is a first player win. In the nim position with piles of sizes , , and , the first player can win by turning the pile of size into a pile of size . Hence in the original game, the first player can win by turning the pile with nim value into a pile with nim value (by taking one stone from that pile).
Example 6.12 Consider the game Subtraction(2,3), in which one is allowed to take or stones from one pile. (Note that the game will end when all piles have size or .) Then we can find the nim sequence using . We find that it is periodic with period 5.
If you were to receive a position with piles of sizes , , and , its nim value would be
Hence it is a first player win, and a winning move is to reduce the pile with nim value to a pile of nim value . This can be done by removing stones from the pile of size to make it have size since .
Slightly more complicated are games in which one is required to break piles into smaller piles.
Example 6.13 In Grundy’s game, a move consists of splitting a pile into two piles of different sizes. (Note that the game will end when all piles have size or since these cannot be split further.)
According to the Mex Rule, we have that . For instance, . We can therefore recursively find the nim sequence of Grundy’s game.
Suppose one were to receive a position with piles of sizes , , and , which have nim values , , and . In such a Nim position, the winning move would be to turn the pile of size into a pile of size . Hence the winning move in the Grundy’s game position is to turn the pile of size into a position with nim value . One way to do this is to split this pile into piles of sizes and since . (One could also split it into and , or and .)
It is not known whether the nim sequence for Grundy’s game eventually becomes periodic.
Example 6.14 The game Kayles is played with a row of pins of length . Players alternate throwing balls one at time at the pins, knocking down either a single pin or two adjacent pins.
From a row of length , one can create two rows of length and if or . Hence
Using this recurrence with the initial condition , we can calculate the nim sequence for Kayles to be
Hence if you receive a position with three rows of sizes , , and :
It is straightforward for a computer to calculate even more values, yielding a nim sequence of