Video 2, chapter 20

Nim

How Nim, a combinatorial game played with several piles of counters, is solved by Bouton's theorem, which writes the pile sizes as binary numbers and says the player to move loses exactly when their nim-sum is zero.

  • Video 2, chapter 20
  • Find the winning move

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 a1,…,aka_1, \dots, a_k for the pile sizes and S=a1⊕⋯⊕akS = a_1 \oplus \cdots \oplus a_k for their nim-sum. The player to move can force a win exactly when S≠0S \ne 0, and the winning moves are exactly the moves that leave a nim-sum of 00. 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 33, 44 and 55. Their nim-sum, worked out in the nim-sum card, is 3⊕4⊕5=23 \oplus 4 \oplus 5 = 2, so the first player can force a win. The only winning first move reduces the pile of 33 to 11, which leaves the nim-sum 1⊕4⊕5=01 \oplus 4 \oplus 5 = 0. The table follows one way the game can go on, with the nim-sum after every move.

MovePiles after itNim-sum
Start3,4,53, 4, 522
You take 22 from the 331,4,51, 4, 500
Opponent takes 11 from the 441,3,51, 3, 577
You take 33 from the 551,3,21, 3, 200
Opponent takes the whole 331,0,21, 0, 233
You take 11 from the 221,0,11, 0, 100
Opponent takes one pile of 110,0,10, 0, 111
You take the last counter0,0,00, 0, 000

After each of your moves the nim-sum is 00, 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 00, which is the balance that matters.

Binary numbers

Definition Every whole number n≥1n \ge 1 can be written in exactly one way as a sum of different powers of two, taken from 1,2,4,8,16,…1, 2, 4, 8, 16, \dots, and the number 00 is the sum of no powers at all. Its binary form records this sum with one digit for each power, 11 if the power is used and 00 if it is not, with the largest power on the left. A subscript 22 marks a binary form, so 1012101_2 means 4+1=54 + 1 = 5. 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 a>ba > b, then at the first place from the left where their binary forms differ, aa has the digit 11 and bb has the digit 00.

In plain words Binary is a way of writing numbers with only the digits 00 and 11, in which each place is worth twice the place to its right.

Example With places worth 44, 22 and 11, the pile sizes of the Nim example are written as follows.

NumberFoursTwosOnesBinary form
330011110112011_2
441100001002100_2
551100111012101_2

A larger number needs more places, for example 13=8+4+1=1101213 = 8 + 4 + 1 = 1101_2. To compare 5=10125 = 101_2 with 3=01123 = 011_2, read from the left. The first digits already differ, and the larger number 55 has the 11 there. The comparison rule holds because a 11 in some place is worth more than all the places to its right together, since 4>2+14 > 2 + 1, and in general 2j>2j−1+⋯+2+12^j > 2^{j-1} + \cdots + 2 + 1 for every j≥1j \ge 1.

Nim-sum

Definition The nim-sum of whole numbers a1,…,aka_1, \dots, a_k, written a1⊕⋯⊕aka_1 \oplus \cdots \oplus a_k, is found by writing the numbers in binary, lined up in columns, and counting the digits 11 in each column. The nim-sum has the digit 11 in each column where the count is odd and 00 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 aa, bb and cc it satisfies

a⊕b=b⊕a,(a⊕b)⊕c=a⊕(b⊕c),a⊕0=a,a⊕a=0.\begin{gathered} a \oplus b = b \oplus a, \\[0.4em] (a \oplus b) \oplus c = a \oplus (b \oplus c), \\[0.4em] a \oplus 0 = a, \\[0.4em] a \oplus a = 0. \end{gathered}

So the order of the numbers does not matter, and a number that appears twice cancels. In particular a⊕b=0a \oplus b = 0 exactly when a=ba = b.

In plain words The nim-sum checks each binary column for an odd or an even number of ones. A nim-sum of 00 means that every column holds an even number of ones.

Example For the piles 33, 44 and 55 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

3⊕4⊕5=0102=2,3 \oplus 4 \oplus 5 = 010_2 = 2,

while the ordinary sum is 3+4+5=123 + 4 + 5 = 12. Since a⊕a=0a \oplus a = 0, two equal piles have nim-sum 00, 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 a1,…,aka_1, \dots, a_k is a losing position for the player to move exactly when

a1⊕a2⊕⋯⊕ak=0.a_1 \oplus a_2 \oplus \cdots \oplus a_k = 0.

Every other position is winning, and a winning move is found as follows. Let S≠0S \ne 0 be the nim-sum, find the leftmost digit 11 of SS in binary, choose a pile aa that also has a 11 in that place, and reduce that pile to a⊕Sa \oplus S counters. Such a pile exists, because that column holds an odd number of ones. The new size is smaller than aa, because a⊕Sa \oplus S agrees with aa to the left of that place and has 00 where aa has 11, 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 00 changes one pile from aa to a different number bb. Some column of aa and bb differs, the count of ones in that column changes by one, and so the new nim-sum a⊕ba \oplus b is not 00. Second, from nim-sum S≠0S \ne 0, changing a pile from aa to b=a⊕Sb = a \oplus S gives the new nim-sum

S′=S⊕a⊕b=S⊕a⊕a⊕S=0,\begin{aligned} S' &= S \oplus a \oplus b \\[0.4em] &= S \oplus a \oplus a \oplus S \\[0.4em] &= 0, \end{aligned}

because nim-summing aa again cancels the old pile and bb 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 00. Suppose a player leaves nim-sum 00. By the first step the opponent cannot leave nim-sum 00, so in particular cannot empty the board, and by the second step the player can leave nim-sum 00 again. Repeating this until the game ends, the player who keeps leaving nim-sum 00 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 00. 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 33, 44 and 55 the nim-sum is S=2=0102S = 2 = 010_2, whose leftmost 11 is in the twos place. Among 0112011_2, 1002100_2 and 1012101_2 only the pile of 33 has a 11 in that place, so the only winning move reduces it to 3⊕2=13 \oplus 2 = 1. For the pile of 55 the formula gives 5⊕2=75 \oplus 2 = 7, which would mean adding counters, so it is not a move. Later in the game of the Nim card, the piles 1,3,51, 3, 5 have nim-sum 7=11127 = 111_2, with leftmost 11 in the fours place. Only 5=10125 = 101_2 has a 11 there, and 5⊕7=25 \oplus 7 = 2, which is the reply of reducing the 55 to 22.

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 11 and 11, the player to move must empty one pile, and the opponent takes the other, so this position is losing. With piles 11 and 22, emptying the pile of 11 or the pile of 22 hands the opponent a single pile to take, but taking one counter from the pile of 22 leaves piles 11 and 11, a losing position for the opponent. So piles 11 and 22 form a winning position, as the nim-sum 1⊕2=31 \oplus 2 = 3 predicts.

Check yourself

  1. The piles hold 22, 55 and 66 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 0102010_2, 1012101_2 and 1102110_2. The fours column holds two ones, the twos column two and the ones column one, so the nim-sum is 0012=1001_2 = 1. It is not 00, so the position is winning. The leftmost 11 of the nim-sum is in the ones place, and only 5=10125 = 101_2 has a 11 there. Reduce that pile to 5⊕1=45 \oplus 1 = 4 by taking one counter. The piles 22, 44 and 66 are 0102010_2, 1002100_2 and 1102110_2, with two ones in the fours column, two in the twos column and none in the ones column, so their nim-sum is 00. This is the only winning move.

  1. The piles hold 66, 1010 and 1515 counters, under normal play. Find every winning move.
Show the answer

In binary, with places worth 88, 44, 22 and 11, the piles are 011020110_2, 101021010_2 and 111121111_2. The eights column holds two ones, the fours column two, the twos column three and the ones column one, so

6⊕10⊕15=00112=3.6 \oplus 10 \oplus 15 = 0011_2 = 3.

The leftmost 11 of the nim-sum is in the twos place, and all three piles have a 11 there, so there are three winning moves.

6⊕3=5,10⊕3=9,15⊕3=12.\begin{aligned} 6 \oplus 3 &= 5, \\[0.4em] 10 \oplus 3 &= 9, \\[0.4em] 15 \oplus 3 &= 12. \end{aligned}

You can take 11 counter from the pile of 66, 11 counter from the pile of 1010, or 33 counters from the pile of 1515. Each leaves nim-sum 00. For example 5⊕10=01012⊕10102=11112=155 \oplus 10 = 0101_2 \oplus 1010_2 = 1111_2 = 15, which cancels the pile of 1515. No other move wins, because a winning move must change some pile aa to exactly a⊕3a \oplus 3.

  1. Switch to misère play, so that taking the last counter loses. Look at the positions with piles 1,11, 1 and with piles 1,1,11, 1, 1. Does the nim-sum still say correctly who wins?
Show the answer

No. The piles 1,11, 1 have nim-sum 00, 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 1,1,11, 1, 1 have nim-sum 11, but under misère play every move leaves 1,11, 1, 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.