The Sprague-Grundy theorem is a statement about impartial games.
In Combinatorial Games - Winning Positions, we analyzed winning positions of impartial games. Several questions arose in trying to find a general characterization for whether a set of nim piles is a winning position or a losing position. Here, we answer these questions by giving the complete characterization for winning and losing positions of nim.
Suppose we have nim piles of sizes . Let be the base 2 representation of . A nim position is losing if over all the numbers in , the number of 1s in each binary position is even and winning otherwise.
Before we prove this statement, let’s look at an example to see what we mean. If the nim piles have sizes , then we can write these numbers as . We want to look at each binary position of these numbers, so let’s arrange them in a table:
We can construct a number that represents the parity of the number of 1's in each binary position. We call this the nim-sum of these numbers. Looking at each column of the table, we see that the 1's column has a single 1, the 10's column has two 1's, and the 100's column and the 1000's column each have a single 1. Our nim-sum here is .
If the piles have sizes , then we can write these numbers as . We want to look at each binary position of these numbers, so let’s arrange them in a table:
The last line represents the parity of the number of 1's, which gives us that the nim-sum of these numbers is 0.
We will modify our description of a winning position to be one where the nim-sum of the pile sizes is not 0, which is equivalent to the original definition. To verify that this accurately describes the set of winning and losing positions, we check 3 things.
First, it is clear that the game with no stones is losing, and has nim-sum 0.
Second, if we are in a losing position, we have nim-sum 0. If we remove stones from a single pile, we are changing the positions of 1's in a single column, which will make the nim-sum no longer 0.
Third, if we are in a winning position, we have nim-sum of the form , where is a binary string. We choose a pile that has a 1 in the same position as the leftmost 1 in the nim-sum. We change this number by flipping each digit for which there is a 1 in the corresponding position in the nim-sum. The nim-sum of these new numbers will be 0, which is a losing position. It is left as an exercise to show that the new number is less than the original number and thus constitutes a valid move.
The nim-sum actually gives us more information than determining whether a position is winning or losing; it also assigns a value to that position. If a position has nim-sum , then from that position it will always be possible to move to a position with nim-sum for any and not possible to move to a position with nim-sum . To see this, consider the proof that any winning position can get to a 0 position, and replace 0 with any smaller number. The described method will extend to this.
We have spent a lot of time developing the theory for a single impartial game, Nim. In fact, this is all the theory that we need to understand any general impartial game! We have the following surprising result:
Any position of an impartial game is equivalent to a nim pile of a certain size.
We can assign a nim-sum to the positions in any impartial game by building up sequentially through small cases. The nim-sum of a position is the smallest non-negative integer such that we cannot move to a position with nim-sum . This requires us to calculate the nim-sum of every position that we can move to. Since the game will always end, this calculation is finite. However, it can get computationally intensive, and a computer may be useful.
The proof of the Sprague-Grundy theorem is identical to our previous proof, where we checked 3 statements.
Two players play a game starting with a pile of stones. On each turn, a player removes from the pile stones. The person who takes the last stone wins. Determine, in terms of and , the nim-sum of each position.
For , the nim-sum of the game with stones will be . This is easy to see inductively starting at . When , this is a losing position, so the nim-sum will be 0. If we repeat this, we see inductively that the nim-sum of and will always be the same. So the nim-sum of stones where we can remove at most is
Odd Nim is like Nim, except you can only remove an odd number of stones from a pile. Determine the nim-sum for a single pile of stones.
Since we can only take an odd number of stones from the pile, the game will always be a losing position if the number of stones is even and a winning position if the number of stones is odd. From a winning position, we can only get to the 0 position, so all winning positions have nim-sum 1. Thus the nim-sum is .