**Proof of the winning formula**

The soundness of the optimal strategy described above was demonstrated by C. Bouton.

**Theorem**. In a normal Nim game, the player making the first move has a winning strategy if and only if the nim-sum of the sizes of the heaps is nonzero. Otherwise, the second player has a winning strategy.

*Proof:* Notice that the nim-sum (⊕) obeys the usual associative and commutative laws of addition (+), and also satisfies an additional property, *x* ⊕ *x* = 0 (technically speaking, the nonnegative integers under ⊕ form an Abelian group of exponent 2).

Let *x*_{1}, …, *x _{n}* be the sizes of the heaps before a move, and

*y*

_{1}, …,

*y*the corresponding sizes after a move. Let

_{n}*s*=

*x*

_{1}⊕ … ⊕

*x*and

_{n}*t*=

*y*

_{1}⊕ … ⊕

*y*. If the move was in heap

_{n}*k*, we have

*x*=

_{i}*y*for all

_{i}*i*≠

*k*, and

*x*>

_{k}*y*. By the properties of ⊕ mentioned above, we have

_{k}t= 0 ⊕t=s⊕s⊕t=s⊕ (x_{1}⊕ ... ⊕x) ⊕ (_{n}y_{1}⊕ ... ⊕y) =_{n}s⊕ (x_{1}⊕y_{1}) ⊕ ... ⊕ (x⊕_{n}y) =_{n}s⊕ 0 ⊕ ... ⊕ 0 ⊕ (x⊕_{k}y) ⊕ 0 ⊕ ... ⊕ 0 =_{k}s⊕x⊕_{k}y(*)_{k}t=s⊕x⊕_{k}y._{k}

The theorem follows by induction on the length of the game from these two lemmas.

**Lemma 1**. If *s* = 0, then *t* ≠ 0 no matter what move is made.

*Proof:* If there is no possible move, then the lemma is vacuously true (and the first player loses the normal play game by definition). Otherwise, any move in heap *k* will produce *t* = *x _{k}* ⊕

*y*from (*). This number is nonzero, since

_{k}*x*≠

_{k}*y*.

_{k}**Lemma 2**. If *s* ≠ 0, it is possible to make a move so that *t* = 0.

*Proof:* Let *d* be the position of the leftmost (most significant) nonzero bit in the binary representation of *s*, and choose *k* such that the *d*th bit of *x _{k}* is also nonzero. (Such a

*k*must exist, since otherwise the

*d*th bit of

*s*would be 0.) Then letting

*y*=

_{k}*s*⊕

*x*, we claim that

_{k}*y*<

_{k}*x*: all bits to the left of

_{k}*d*are the same in

*x*and

_{k}*y*, bit

_{k}*d*decreases from 1 to 0 (decreasing the value by 2

^{d}), and any change in the remaining bits will amount to at most 2

^{d}−1. The first player can thus make a move by taking

*x*−

_{k}*y*objects from heap

_{k}*k*, then

t=s⊕x⊕_{k}y(by (*)) =_{k}s⊕x⊕ (_{k}s⊕x) = 0._{k}

The modification for misère play is demonstrated by noting that the modification first arises in a position that has only one heap of size 2 or more. Notice that in such a position *s* ≠ 0, therefore this situation has to arise when it is the turn of the player following the winning strategy. The normal play strategy is for the player to reduce this to size 0 or 1, leaving an even number of heaps with size 1, and the misère strategy is to do the opposite. From that point on, all moves are forced.

**Useful Links:**

## Leave a Reply