You are on page 1of 28

THE GAME OF NIM

And its Winning Strategy


By Jason Jebbia

The Game of Nim

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

History of Nim

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

Rules of Nim

Game starts with a number of piles (or


rows, columns, etc.) and a number of
objects (pennies, peanuts, counters, etc.)
in each pile.
Example

Rules of Nim

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

Rules of Nim

Two ways to play:


Normal Play: the player who removes the last
object is the winner
Misre Play: the player who removes the last
object is the loser

Winning Strategy

*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

Winning Strategy

Example

Nim
Sum

Binary
7 = 111

4 = 100

5 = 101
110

Winning/Losing Positions

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.

Winning Strategy

7 = 111
4 = 100
5 = 1 0 11 1 0

0
1
1
0

0
0
0
0

1= 1
0
= 4
1= 5
0

1
0
1
0

1
1
0
0

1= 7
0
= 2
1= 5
0

1
1
0
0

1
0
1
0

1= 7
0
= 4
1= 3
0

Winning Strategy

Intuition

If the Nim sum is greater than 0, then we


know that at least one of the column sums is
1.

7 = 11
4 = 10
5 = 10
11

1
0
1
0

Winning Strategy

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 = 111
1 1 1= 7
8 = 100
0 1 0= 2
9 = 101
1 0 1= 5
110
000

Winning Strategy

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

Winning Strategy

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 =

0
1
1
0

0
0
0
0

1
0
1
0

Winning Strategy

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).

Winning Strategy

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,

Misre Nim

The player who removes the last object


loses!
Therefore, the goal is to leave the last
object for your opponent to take
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.

Misre Nim
1
0
0
1

0
1
0
1

0= 4 0 1 1 = 3
0 = 2 0 1 0= 2
1= 1 0 0 1= 1
10 0 0

0
0
0
0

0
1
0
1

1= 1 0 0 1= 1
0 = 2 0 0 1= 1
1= 1 0 0 1= 1
0

0 0 0= 0 0 0 0= 0
0 0 1 = 1 0 0 1= 1
0 0 1= 1 0 0 0= 0

Misre Nim
1
0
0
1

0
1
0
1

0= 4 0 1 1 = 3
0 = 2 0 1 0= 2
1= 1 0 0 1= 1
10 0 0

0
0
0
0

1
1
0
0

0= 2 0 0 1= 2
0 = 2 0 0 1= 2
1= 1 0 0 0= 0
10 0 0

0
0
0
0

0
1
0
1

1= 1 0 1 0= 1
0 = 2 0 0 0= 0
0= 0 0 0 0= 0
1

Misre Nim

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 cant 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.

Bounded Nim

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!

Bounded Nim

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.

Bounded Nim

Example : k = 4

Pile Sizes
11
14
9
13

Residue Piles
Binary
= 1 (mod 5)
001
= 4 (mod 5)
100
= 4 (mod 5)
100
= 3 (mod 5)
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!

Importance of Nim

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

Importance of Nim

Sprague-Grundy Theorem:
Every impartial game in its normal play
convention is equivalent to some Nim pile
using the Sprague-Grundy 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)

What have we learned?

The Game of Nim


The winning strategy of Nim
Misre and Bounded Nim
The Sprague-Grundy Theorem of
Impartial Games

Special Thanks

To:
Professor Tamas Lengyel
Professor Ron Buckmire
Professor Eric Sundberg

References

[1] E. R. Berlekamp, J. H. Conway and R. K. Guy (1982) Winning


Ways for your mathematical plays, vols. 1 and 2, Academic
Press, 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

You might also like