In our previous post 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. In this post, 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 1s 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, 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.
Proof: 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 1s in a single column, which will make the Nim Sum no longer be 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 so 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:
The Sprague-Grundy Theorem. 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.
1. 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.
Solution: 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. And 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
2. 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.
Solution: 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 .