Three piles hold 3, 4 and 5 counters. On each turn a player takes as many counters as they like from one pile, and whoever takes the last counter wins. With a single pile the first player would simply take everything, but with three piles every move leaves the opponent a new set of choices. This page finds a winning first move by writing the pile sizes in binary, and it proves that the same rule finds a winning move from every position where a win can be forced.
Key terms
Nim
Definition Nim is a game for two players with a finite number of piles of counters. The players take turns. On each turn a player chooses one pile that is not empty and removes any positive number of counters from it, from one counter up to the whole pile. No other pile changes, and no pile ever grows. Both players see all the piles, and nothing is decided by chance. Under normal play, the rule used on this page, the player who takes the last counter wins, so a player who faces only empty piles has lost.
The game is solved by Bouton’s theorem, stated in its own card below. Write for the pile sizes and for their nim-sum. The player to move can force a win exactly when , and the winning moves are exactly the moves that leave a nim-sum of . Under the misère rule, in which taking the last counter loses, this strategy needs a change near the end of the game.
In plain words Nim is a take-away game with several piles and no limit on how much you take, except that one move touches only one pile. The total number of counters does not decide who wins. What matters is a kind of balance between the piles that shows up when their sizes are written in binary.
Example Start with piles of , and . Their nim-sum, worked out in the nim-sum card, is , so the first player can force a win. The only winning first move reduces the pile of to , which leaves the nim-sum . The table follows one way the game can go on, with the nim-sum after every move.
| Move | Piles after it | Nim-sum |
|---|---|---|
| Start | ||
| You take from the | ||
| Opponent takes from the | ||
| You take from the | ||
| Opponent takes the whole | ||
| You take from the | ||
| Opponent takes one pile of | ||
| You take the last counter |
After each of your moves the nim-sum is , and after each of the opponent’s moves it is not. Your reply in the fourth row does not restore equal piles or the old heights. It restores a nim-sum of , which is the balance that matters.
Binary numbers
Definition Every whole number can be written in exactly one way as a sum of different powers of two, taken from , and the number is the sum of no powers at all. Its binary form records this sum with one digit for each power, if the power is used and if it is not, with the largest power on the left. A subscript marks a binary form, so means . Zeros on the left change nothing, so several numbers can be written with the same number of digits and lined up in columns, one column for each power. Two different numbers can be compared in binary by reading from the left. If , then at the first place from the left where their binary forms differ, has the digit and has the digit .
In plain words Binary is a way of writing numbers with only the digits and , in which each place is worth twice the place to its right.
Example With places worth , and , the pile sizes of the Nim example are written as follows.
| Number | Fours | Twos | Ones | Binary form |
|---|---|---|---|---|
A larger number needs more places, for example . To compare with , read from the left. The first digits already differ, and the larger number has the there. The comparison rule holds because a in some place is worth more than all the places to its right together, since , and in general for every .
Nim-sum
Definition The nim-sum of whole numbers , written , is found by writing the numbers in binary, lined up in columns, and counting the digits in each column. The nim-sum has the digit in each column where the count is odd and in each column where it is even. The same operation is called bitwise exclusive or, or XOR. It is not ordinary addition, because nothing is carried from one column to the next. For all whole numbers , and it satisfies
So the order of the numbers does not matter, and a number that appears twice cancels. In particular exactly when .
In plain words The nim-sum checks each binary column for an odd or an even number of ones. A nim-sum of means that every column holds an even number of ones.
Example For the piles , and in the table of the binary card, the fours column holds two ones, the twos column one and the ones column two. Only the twos column is odd, so
while the ordinary sum is . Since , two equal piles have nim-sum , which matches a simple strategy for two piles. Hand your opponent two equal piles, and then copy each of their moves on the other pile. The piles are equal again after each of your moves, so the opponent can never take the last counter, and you do.
Bouton’s theorem
Definition Bouton’s theorem says that in Nim under normal play, the position with piles is a losing position for the player to move exactly when
Every other position is winning, and a winning move is found as follows. Let be the nim-sum, find the leftmost digit of in binary, choose a pile that also has a in that place, and reduce that pile to counters. Such a pile exists, because that column holds an odd number of ones. The new size is smaller than , because agrees with to the left of that place and has where has , so the move is legal. The winning moves are exactly the moves of this kind.
The proof has three steps. First, a move from nim-sum changes one pile from to a different number . Some column of and differs, the count of ones in that column changes by one, and so the new nim-sum is not . Second, from nim-sum , changing a pile from to gives the new nim-sum
because nim-summing again cancels the old pile and brings in the new one. Third, each move removes at least one counter, so the game ends after finitely many moves, at the empty position, whose nim-sum is . Suppose a player leaves nim-sum . By the first step the opponent cannot leave nim-sum , so in particular cannot empty the board, and by the second step the player can leave nim-sum again. Repeating this until the game ends, the player who keeps leaving nim-sum is the one who takes the last counter. The theorem is named after Charles L. Bouton, who published it in the early 1900s.
In plain words Call a position balanced when its nim-sum is . Every move unbalances a balanced position, and every unbalanced position has a move that balances it again. The player who keeps handing over balanced positions is the one who empties the board.
Example From piles , and the nim-sum is , whose leftmost is in the twos place. Among , and only the pile of has a in that place, so the only winning move reduces it to . For the pile of the formula gives , which would mean adding counters, so it is not a move. Later in the game of the Nim card, the piles have nim-sum , with leftmost in the fours place. Only has a there, and , which is the reply of reducing the to .
Combinatorial game
Definition A combinatorial game, in the sense used here, is a game for two players who move alternately and both know the whole position at every moment, with no dice, shuffled cards or other chance. The rules say which moves are legal from each position, every play ends after finitely many moves, and the game cannot end in a draw, because a rule such as normal play decides the winner. Such a game is impartial when both players have the same moves available from every position, and partisan when the available moves depend on whose turn it is. Backward induction shows that every position of a combinatorial game is either a winning or a losing position for the player to move. Some authors use the term more broadly and include games that can end in a draw, such as tic-tac-toe.
In plain words A combinatorial game is a game of pure reasoning. Nothing is hidden and nothing is left to luck, so with perfect play the result is fixed before the first move.
Example Nim, the take-away game and Chomp are impartial combinatorial games. Backgammon is not one, because dice decide which moves are possible, and poker is not one, because players hide their cards. Backward induction settles small Nim positions directly. With piles and , the player to move must empty one pile, and the opponent takes the other, so this position is losing. With piles and , emptying the pile of or the pile of hands the opponent a single pile to take, but taking one counter from the pile of leaves piles and , a losing position for the opponent. So piles and form a winning position, as the nim-sum predicts.
Check yourself
- The piles hold , and counters, under normal play. Is this a winning position for the player to move? If it is, find a winning move.
Show the answer
In binary the piles are , and . The fours column holds two ones, the twos column two and the ones column one, so the nim-sum is . It is not , so the position is winning. The leftmost of the nim-sum is in the ones place, and only has a there. Reduce that pile to by taking one counter. The piles , and are , and , with two ones in the fours column, two in the twos column and none in the ones column, so their nim-sum is . This is the only winning move.
- The piles hold , and counters, under normal play. Find every winning move.
Show the answer
In binary, with places worth , , and , the piles are , and . The eights column holds two ones, the fours column two, the twos column three and the ones column one, so
The leftmost of the nim-sum is in the twos place, and all three piles have a there, so there are three winning moves.
You can take counter from the pile of , counter from the pile of , or counters from the pile of . Each leaves nim-sum . For example , which cancels the pile of . No other move wins, because a winning move must change some pile to exactly .
- Switch to misère play, so that taking the last counter loses. Look at the positions with piles and with piles . Does the nim-sum still say correctly who wins?
Show the answer
No. The piles have nim-sum , so the normal-play rule would call them losing for the player to move. Under misère play, the player to move empties one pile, and the opponent must take the last counter and loses, so the position is winning. The piles have nim-sum , but under misère play every move leaves , the opponent empties one pile, and the player who started must take the last counter, so the position is losing.
The proof of Bouton’s theorem used the normal-play ending, in which the empty position is a loss for the player to move, and misère play reverses that ending. The known fix changes only the end of the game. Play exactly as in normal Nim until your move would leave no pile with two or more counters, and at that move leave an odd number of piles of one counter instead of an even number.