## Application: Winning the game of Nim

The game of nim is very simple to play and has an interesting mathematical structure. Nim is a game for two players, who alternate taking turns to play. Initially the players are given a state'' or position'' consisting of several piles of tokens. On each turn a player picks a pile and then removes at least one token from that pile. The player who picks up the last token wins 1.2. Assuming your opponent plays optimally, some positions cannot be won. Such positions are called losing (for the player whose turn it is to move). The positions which are not losing are called winning. (In [BCG], a winning position is defined for the player who just moved.)

The following question has a mathematical answer.

Question: Which positions are losing for the player whose turn it is to move?

The answer is determined by the nim sum'' of the piles. The nim sum'' of piles with elements in the pile, elements in the pile, ..., elements in the last ( ) pile, is obtained as follows:

1. Write each of these numbers in binary and represent this binary number as a vector of 0's and 's with 0's filled in on the end to make them all the same length: so that ( ),

2. Define the nim sum vector of the position vector to be the vector sum (componentwise addition).

3. Define the nim sum of the position to be the number whose binary digits are the entries in the nim sum vector.

If each of the sums (for ) is even then the nim sum is 0 and we call the position even. Otherwise, we call the position odd. In the book by Ball and Coxeter [BC], one finds the following result.

Theorem 1.3.4   Odd positions are winning and even positions cannot be won if your opponent plays optimally.

If an initial position is not a losing position, what strategy should one use to win? Make a move which leaves your opponent an even position. In the special case when you only have two piles, a simpler version of this strategy is simply to make the two piles equal by taking tokens from the larger pile.

Example 1.3.5   Suppose Laura plays Robert and that the initial position is which is an even position (the nim sum vector is ). Laura has graciously allowed Robert to play first and he removes one token from the first pile. Laura plays by removing the one (and only) token from the third pile. The position is now which is again an even position (the nim sum vector is ). Robert plays by removing another token from the first pile. Laura plays by removing one token from the second pile. The position is now (the nim sum vector is ). Robert removes one token from one pile and Laura wins by taking the last remaining token.

For more detailed answers to these questions, see the references [BC] or [HW].

Exercise 1.3.6   Show is in binary, is in -ary, is in -ary, and is in -ary.

Exercise 1.3.7 (Bachet's measuring problem)   An assayer owns a balance scale for ore smaples. He places samples on the left scale, weights on the right scale until balance is achieved. He wants to weight all possible samples of grams 1 to n. What is the smallest number of (integral) weights he will need?

Exercise 1.3.8   Use the above rules to answer the following questions. Is 1234567887654321 divisible by 3? 7? 9? 11? 13?

Exercise 1.3.9   Use the above rules to answer the following questions. Is 12345678 divisible by 3? 4? 7? 8? 9? 12?

Exercise 1.3.10   Use the above rules to answer the following questions. Is 1002003004005006 divisible by 3? 4? 7? 8? 9? 11? 12? 13?

Exercise 1.3.11   One quick way to tell if a number was divisible by 7, is as follows. Take the number (expressed in base 10, as are all numbers in the fifth grade), and temporarily put aside the two rightmost digits. Double the number that remains, and add this result to the two-digit number that was just removed. This new result will leave the same remainder as the original number, when divided by 7.

Example: The remainder when 1234 is divided by 7 is the same as . Clearly the remainder should therefore be 2.

(a)
Express this divisibility test'' in the same form as those above, namely fill in the blank: if and only if _____________''

(b)
Prove that this test indeed works.

(c)
Invent a related divisibility test for 17. [HINT: Here, consider subtracting rather than adding.]

Exercise 1.3.12   Show that if the starting position in a game of nim is , then the second player can always win.

Exercise 1.3.13

Find the nim sum vector for the position Exercise 1.3.14   By enumerating all possible games starting from the position show that the second player can always win.

David Joyner 2007-09-03