Roland Sprague, a mathematician in Germany, published this theorem in 1935. Patrick Grundy, then a student at Cambridge, found it again in 1939 and published it in Eureka, the student mathematics magazine there. Neither knew about the other's work, which is why the theorem carries both names. In 1982 Elwyn Berlekamp, John Conway, and Richard Guy published Winning Ways for Your Mathematical Plays, which uses this theory to solve hundreds of games, Kayles included. It has jokes, pictures, and serious mathematics on the same page, and it is the book to read when you are about 15.
Every game is secretly Nim
Chapter 5 labeled positions W or L. Chapter 6 turned Nim piles into binary. This chapter joins them: every position in every game like these is secretly a Nim pile, and one number, its Grundy value, tells you how big. Once you can find that number, you can play several games at once and still beat Robo.
Chapter 5 gave every position a letter: W if the player whose turn it is can force a win, L if they cannot. Chapter 6 gave every Nim position a number: write the piles in binary, add the columns without carrying, and if that nim-sum is 0 you are standing on an L. Two different tools, and it looked as if the second one only worked for Nim.
Here is a puzzle that breaks the first tool. Put two games on the table at once. Game A is a pile of 6 stones where a move takes 1, 2, or 3. Game B is a pile of 4 stones where a move takes 1, 3, or 4. On your turn you move in one of the games, whichever you like, and if you cannot move anywhere, you lose. Each game on its own is a W for the player to move (6 mod 4 is not 0, and 4 is not an L position from Chapter 5's challenge). Put them together and the player to move loses. Change Game B to 5 stones, still a W on its own, and the player to move wins. W plus W is sometimes L and sometimes W, and the letters cannot tell you which.
You need something finer than a letter: a number. Finding it takes one new idea with a funny name.
A number for every position
Take any set of whole numbers and look for the smallest whole number that is not in it. For {0, 1, 2} the answer is 3. For {1, 2, 3} the answer is 0, because 0 is missing. For {0, 1, 3, 4} it is 2. This is the mex of the set (say: mex, rhymes with checks), short for minimum excluded value. Repeats do not matter and neither does the order: the mex of {3, 0, 0, 1} is 2. The mex of the empty set, the set with nothing in it, is 0: 0 is the first number you look for, and it is not there.
Try the calculator, then do the five problems without it.
The mex calculator
Type a set. The smallest missing whole number lights up.The Grundy rule
Now give every position a number, called its Grundy value (say: GRUN-dee) and written G. The rule looks like the W and L rule from Chapter 5, with mex doing the work:
G(position) = the mex of the Grundy values of every position you can move to.
A position with no moves left has G = 0: the mex of the empty set.
Try it on the 21 Game, takes of 1, 2, or 3. From 0 there are no moves, so G(0) = 0. From 1 the only move is to 0, so G(1) = mex{0} = 1. From 2 you can reach 1 and 0, whose values are 1 and 0: mex{1, 0} = 2. From 3: mex{2, 1, 0} = 3. From 4 you can reach 3, 2, and 1, whose values are 3, 2, and 1. The set is {1, 2, 3}, 0 is missing, so G(4) = 0. Keep going and the values run 0, 1, 2, 3, 0, 1, 2, 3, and so on forever. You have seen that pattern before: G(n) = n mod 4.
Look at where the zeros landed: 0, 4, 8, 12. Those are exactly the L positions from Chapter 5. That is not luck. G = 0 means L, and G > 0 means W, in every game, and two sentences say why. If G = 0, then 0 is missing from the set of values you can reach, so every move hands the other player a nonzero position. If G > 0, then 0 is not the mex, so 0 must be in the set: some move reaches a position worth 0, and you hand that one over. That is the W and L rule with 0 playing the part of L. A position with no moves is both L and 0, so the two labelings agree from the end of the game all the way back to the start.
So the Grundy values know everything the letters knew, and more: they tell a 1 from a 2 from a 3, and the next section is where that pays off. First, label a game yourself. Type each value or use the small buttons. "Check" marks the wrong cells without giving answers, and "Show me" walks through the mex reasoning one position at a time.
Label the positions with Grundy values
G(n) = mex of the values one move away. G(0) = 0.Playing two games at once
Back to the puzzle at the top. Playing two games side by side is called a sum of games, and the rules of a sum are exactly what you would guess. On your turn you make one legal move in exactly one of the games. You lose when it is your turn and neither game has a move left. Nim itself is a sum: each pile is a tiny game of its own, and a Nim pile of n stones has Grundy value n, because from n you can reach every smaller pile, and the mex of {0, 1, ..., n − 1} is n.
Here is the theorem this chapter is named for.
Take any game where two players take turns, both players have the same moves, there is no chance and nothing hidden, the game always ends, and the last player to move wins. Then every position has a Grundy value, and for two games played at once,
G(A + B) = G(A) XOR G(B).
Games that fit that description are called impartial games (say: im-PAR-shul). The important word is impartial: both players have the same moves from every position. Every subtraction game is impartial, and so is Nim. Chess is not, because you may only move your own pieces.
Read the theorem again slowly, because it says something surprising. A position with Grundy value 2 combines with other games exactly the way a Nim pile of 2 stones would. That is the sense in which every game is secretly Nim. To play a sum of games, replace each game by a Nim pile of size G, then use Chapter 6's rule: move so that the nim-sum becomes 0.
Now the puzzle at the top makes sense. Game A, 6 stones with takes of 1, 2, 3, has G = 6 mod 4 = 2. Game B, 4 stones with takes of 1, 3, 4, has G = 2 as well (the Math corner works it out). Two piles of 2: 2 XOR 2 = 0, a loss for the player to move. Change B to 5 stones and its value becomes 3. Now 2 XOR 3 = 1, a win, and the winning move is to bring B's value down from 3 to 2 by taking 1 stone. The two games have different rules and you cannot copy a move from one into the other, but the numbers see straight through that.
Play it. Robo does this arithmetic silently unless you tick "Show the secret". The hint tells you which game to fix, what value to aim for, and which stones get you there.
Two games at once
Move in one game per turn. If you cannot move anywhere, you lose.Game A
Game B
The formulas. For a set S of whole numbers, mex(S) is the smallest whole number not in S. If a position p has options p1, p2, ..., pk (the positions one move away), then
and for two games played at once
where ⊕ (say: oh-plus) is the nim-sum from Chapter 6: write both numbers in binary and add each column without carrying, which is the same as XOR. Mathematicians call Grundy values nimbers, and in some books a Nim pile of 3 is written *3 and read "star three".
The starting position on the board. Game A: 10 stones, takes {1, 2, 3}. The values from 0 upward are 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, so G(10) = 2. Game B: 9 stones, takes {1, 3, 4}. Work upward one row at a time:
| n | can reach | their values | G(n) |
|---|---|---|---|
| 0 | nothing | { } | 0 |
| 1 | 0 | {0} | 1 |
| 2 | 1 | {1} | 0 |
| 3 | 2, 0 | {0, 0} | 1 |
| 4 | 3, 1, 0 | {1, 1, 0} | 2 |
| 5 | 4, 2, 1 | {2, 0, 1} | 3 |
| 6 | 5, 3, 2 | {3, 1, 0} | 2 |
| 7 | 6, 4, 3 | {2, 2, 1} | 0 |
| 8 | 7, 5, 4 | {0, 3, 2} | 1 |
| 9 | 8, 6, 5 | {1, 2, 3} | 0 |
The sequence 0, 1, 0, 1, 2, 3, 2 then repeats every 7, and its zeros (0, 2, 7, 9, 14, 16, ...) are exactly the L positions from Chapter 5's challenge. So G(9) = 0. The starting position is worth 2 ⊕ 0 = 2, in binary 10 ⊕ 00 = 10, so the player to move wins. Which move? Chapter 6's rule: the nim-sum's leftmost 1 is in the 2s column, and Game A's value has a 1 there, so fix Game A. Aim it at 2 ⊕ 2 = 0: take 2 stones, leaving 8, which is worth 0. Now the position is worth 0 ⊕ 0 = 0, and Robo is on the losing side.
Why XOR? Here is the idea of the proof, in three steps.
- A Nim pile of g stones can move to any smaller pile: 0, 1, ..., g − 1. A position with Grundy value g can also reach every value below g (they are all in its set, or g would not be the mex), and it can never reach a value of g itself.
- The only difference is that a position might also have moves to values bigger than g. Those moves never help the player who makes them: if your opponent jumps from g up to a bigger value h, you can move straight back down to a position worth g, because g is below h. The value is where it was, and since the game always ends, the bouncing cannot go on forever.
- So inside a sum, a position worth g is a Nim pile of g plus some extra moves that never help. Whoever wins Nim with piles G(A) and G(B) wins A + B by the matching moves, and Chapter 6 says the Nim winner is decided by the nim-sum.
Every position in an impartial game is secretly a Nim pile. Its size is the Grundy value: the mex of the values one move away, and 0 for a position with no moves. To play several games at once, nim-sum the Grundy values, and move so that the nim-sum becomes 0.
1. Find the mex of {0, 1, 2, 4, 5}, and the mex of {1, 2, 3, 4}.
Answer
3 and 0. In the second set 0 is missing, and the mex does not care how many bigger numbers are present.
2. In the subtraction game where a move takes 2 or 3 stones (never 1), find G(0) through G(8). Careful: 1 is a position with no moves.
Answer
0, 0, 1, 1, 2, 0, 0, 1, 1. From 1 you cannot move, so G(1) = 0, and 1 is an L position: the player who faces a single stone is stuck. From 4 you can reach 2 and 1, worth 1 and 0, so G(4) = 2. From 5 you reach 3 and 2, both worth 1, so G(5) = 0. The pattern repeats every 5.
3. You face a sum: a pile of 6 stones with takes of 1, 2, 3, next to a plain Nim pile of 2 stones. It is your turn. Who wins, and what is the move?
Answer
G(6) = 6 mod 4 = 2, and the Nim pile is worth 2. 2 ⊕ 2 = 0: you are on a losing position, and there is no winning move. Whatever you do, Robo answers by bringing the nim-sum back to 0: take 1 from the 6 (now worth 1) and Robo takes 1 from the Nim pile (now worth 1). If the Nim pile had 3 stones instead, 2 ⊕ 3 = 1 and you would win by taking 1 from it.
Kayles. A row of bowling pins. On your turn you knock down one pin, or two pins standing next to each other. Last move wins. Knocking down a pin in the middle of a row splits it into two shorter rows that never touch again, and that is a sum. So one number per row length is enough to play perfectly: G(row of n) is the mex, over every possible knock-down, of the nim-sum of the two pieces left behind. Work out the values for rows of length 0 to 8. Then say who wins a row of 5 pins, and what the first move is. The board below will check you.
Answer
The values are 0, 1, 2, 3, 1, 4, 3, 2, 1. For a row of 4, for example: an end pin leaves a row of 3 (worth 3); a middle pin leaves rows of 1 and 2 (1 ⊕ 2 = 3); the two end pins leave a row of 2 (worth 2); the middle two leave rows of 1 and 1 (1 ⊕ 1 = 0). mex{3, 3, 2, 0} = 1.
A row of 5 is worth 4, so the first player wins. The move: knock down the middle pin, leaving 2 + 2, worth 2 ⊕ 2 = 0, then copy whatever Robo does in the other row. That mirror trick works for a single row of any length (middle pin if odd, middle pair if even), so every single row is a first-player win. The values still earn their keep: once the rows are unequal, only the numbers can tell you what to do. Richard Guy and Cedric Smith computed the Kayles values in 1956 using exactly this theorem. Beyond 70 pins the values repeat every 12.
Kayles
Knock down one pin, or two standing next to each other. Last move wins.Stars in this chapter
Earn them by doing the clever thing, not by clicking around.
- Get all five mex problems right
- Grundy-label a subtraction game up to 12 or more
- Beat a perfect Robo at a sum of two games