### The Game of Nim and Its Winning Strategy

```And its Winning Strategy
By Jason Jebbia

In this presentation I will:
Introduce the game play and rules of the game of
Nim
 Demonstrate the winning strategy of Nim
 Discuss other variations of Nim including their
winning strategies.
 Discuss the importance of Nim to other impartial
games using the Sprague-Grundy Theorem



Origins of the game are uncertain
Charles Bouton, 1901


Coined the name “Nim” for the game
Developed a complete theory of the game, including
a winning strategy


Game starts with a number of piles (or rows,
columns, etc.) and a number of objects
(pennies, peanuts, counters, etc.) in each pile.
Example




Two Player Game
Players take turns
On each turn, a player must choose one pile
and remove at least one object from the chosen
pile.
Example

Two ways to play:


Normal Play: the player who removes the last object
is the winner
Misère Play: the player who removes the last object
is the loser




*For the normal play convention
Convert the size of each pile into its binary
notation
Add the columns up independently modulo 2.
The resulting value is called the Nim sum

Example

7 =
Binary
111

4 =
100

5 =
101
110
Nim Sum





A winning position occurs when the Nim sum
equal zero
A losing position occurs when the Nim sum is
greater than zero
A player can always make a move from a losing
position to a winning one.
Once in a winning position, the next move will
always result in a losing position
Therefore, the optimal strategy is to always be
converting losing positions to winning positions
on each move.



7
4
5
=
=
=
111
100
101
110
001
100
101
000
=
=
=
1
4
5
111
010
101
000
=
=
=
7
2
5
111
100
011
000
=
=
=
7
4
3

Intuition

If the Nim sum is greater than 0, then we know that
at least one of the column sums is 1.
7
4
5
=
=
=
111
100
101
110

Intuition

Thus, a player can convert a losing position into a
winning one by changing the digits in the columns
where the Nim sums are 1 starting with the left most
column where the column sum is 1.
7 =
4 =
5 =
111
100
101
110
111 =
010 =
101 =
000
7
2
5

Intuition:

If a player moves to a winning position, then the
Nim sum equals 0. If this is not the final position (no
objects left on the table), then every digit that is a 1
will have a corresponding 1 in a different row that
will result in a column Nim sum of 0.
001
100
101
000

Intuition

Thus, any move from a winning position will result
in a losing position, then, since a player will change
one, and only one, row of digits on a given move
creating a new Nim sum.
1
4
5
=
=
=
001
100
101
000

Two Absolutes:



1. A player cannot move from a winning position
back to a winning position (Nim Sum = 0)
2. Every losing position (Nim Sum > 0) has an
allowable move to a winning position.
Thus the winning strategy is to continue
making moves converting losing positions to
winning positions until the ultimate winning
position is achieved (No more objects on the
table).




Of course, this assumes the other player does
not know the winning strategy as well.
If both players know the winning strategy and
play without error, then the winner can be
determined by the starting position of the
game.
If the game starts in a losing position, then the
first player to move will ultimately win.
If the game starts in winning position, then the
second player to move will ultimately win.



The player who removes the last object loses!
Therefore, the goal is to leave the last object for
Winning Strategy:


Play exactly like you would in normal play until
your opponent leaves one pile of size greater than
one.
At this point, reduce this pile to size 1 or 0,
whichever leaves an odd number of piles with only
one object.
100
010
001
111
=
=
=
4
2
1
011
010
001
000
=
=
=
3
2
1
001
010
001
010
=
=
=
1
2
1
001
001
001
=
=
=
1
1
1
000
001
001
=
=
=
0
1
1
000
001
000
=
=
=
0
1
0
100
010
001
111
=
=
=
4
2
1
011
010
001
000
=
=
=
3
2
1
010
010
001
001
=
=
=
2
2
1
001
001
000
000
=
=
=
2
2
0
001
010
000
011
=
=
=
1
2
0
010
000
000
=
=
=
1
0
0

Why this works



Optimal play from normal Nim will never leave you
with exactly one pile of size greater than one
(because this leaves a losing position).
Your opponent can’t move from two piles of size
greater than one to no piles of size greater than one.
So eventually your opponent must make a move that
leaves only one pile of size greater than one.



Same game as the normal version of Nim
except one restriction.
Restriction: An upper limit, k, is placed on the
amount of objects that may be selected on a
given turn. So a player may remove 1≤ m ≤ k
objects on each turn.
Once again, the winning strategy is very much
like the normal game!

Winning Strategy:



Convert each pile into a “residue pile” by reducing
each pile modulo (k + 1).
Perform the same Nim Sum operation on these new
piles
Then, make the same moves to the actual piles that
you would to make the residue piles into a Nim sum
of 0.

Example : k = 4
Pile Sizes
11
14
9
13

Residue Piles
= 1 (mod 5)
= 4 (mod 5)
= 4 (mod 5)
= 3 (mod 5)
Binary
001
100
100
011
010
The Nim sum is not 0, so this is a losing position.
Therefore, we would continue to use the same strategy
as we would in normal play to make the Nim sum of
the residue piles equal zero!


Nim is an Impartial game.
Impartial game:

Combinatorial Game
 Two-players, Perfect Information, no moves are left to chance
(so poker is not a combinatorial game)
* Any play available to one player must be available to
the other as well
 Examples: Hackenbush, Kayles, and Sprouts


Partizan Game
Combinatorial Game
 *Players have different sets of possible moves
 Examples: Chess, Checkers, and Go


Sprague-Grundy Theorem:



Every impartial game in its normal play convention
is equivalent to some Nim pile using the SpragueGrundy Function
Using this Sprague Grundy function, every impartial
game gets assigned a Nim pile value that allows us
to analyze the game.
Discovered independently by R. P. Sprague (1935)
and P. M. Grundy (1939)




The Game of Nim
The winning strategy of Nim
Misère and Bounded Nim
The Sprague-Grundy Theorem of Impartial
Games

To:



Professor Tamas Lengyel
Professor Ron Buckmire
Professor Eric Sundberg







[1] E. R. Berlekamp, J. H. Conway and R. K. Guy (1982) Winning
New York.
[2] C. L. Bouton (1902) Nim, a game with a complete mathematical
theory, Ann. Math. 3, 35-39.
[3] J. H. Conway (1976) On Numbers and Games, Academic Press,
New York.
[4] E. H. Moore (1910) A generalization of a game called nim, Ann.
Math. 11, 93-94
[5] J. G. Baron (1974) The Game of Nim-A Heuristic Approach.
Mathematics Magazine 47.1, 23-28.
[6] B. L. Schwartz (1971) Extensions of NIM, Mathematics Magazine
44.5, 252-57.
[7] G.H. Hardy and E.M. Wright (1960), An Introduction to the
Theory of Numbers, 4th ed., Oxford, Clarendon Press, 117-120
```