Impartial games

Grundy sequences, and where they stop being predictable

Computing one Grundy value is a mex. Computing all of them produces a sequence, and the sequences do something nobody has fully explained — most of them eventually repeat, some of them take thousands of terms to start, and for a few nobody knows whether they ever do.

Assumes: Every impartial game is a Nim heap · Nim, and the nim-sum

Sprague–Grundy says every impartial position is a Nim heap and gives a recursion for finding which one. For a game played on heaps, running that recursion over every heap size produces a sequence — the Grundy value of a heap of zero, of one, of two, and onwards.

That sequence is the complete solution of the game. It is also, in several well-studied cases, an object nobody understands.

Grundy values for subtraction of 1, 3, 4. The Grundy value of every heap size for a take-away game, computed by the mex rule. A period, if the figure marks one, was found by searching the computed sequence rather than assumed — and where no period is marked, none was found in the range drawn, which is not the same as there being none.
Fig. 1 The subtraction game where a move takes one, three or four counters. Each cell is the mex of the cells its rules can reach, and the whole sequence repeats with period seven from the very beginning.

The simplest sequences

Start with subtraction games, where a move removes a number of counters from the chosen set SS.

For S={1}S = \{1\}, the sequence is 0,1,0,1,0,1,0, 1, 0, 1, 0, 1, \ldots — period two, and the game is just parity. For S={1,2}S = \{1, 2\}, it is 0,1,2,0,1,2,0, 1, 2, 0, 1, 2, \ldots — period three, and the Grundy value is the heap size modulo three. In general, for S={1,2,,k}S = \{1, 2, \ldots, k\} the sequence is the heap size modulo k+1k+1, which makes the game a thin disguise for Nim with a move limit.

Grundy values for subtraction of 1, 2, 3. The Grundy value of every heap size for a take-away game, computed by the mex rule. A period, if the figure marks one, was found by searching the computed sequence rather than assumed — and where no period is marked, none was found in the range drawn, which is not the same as there being none.
Fig. 2 The move-limited case. The values are the heap size modulo four, the period is as short as it can be, and the game is completely transparent.

These are the cases where the sequence can be predicted without computing it. They are also, essentially, all of them.

The moment prediction fails

Change the set to {1,3,4}\{1, 3, 4\} and the sequence becomes

0,1,0,1,2,3,2,  0,1,0,1,2,3,2,  0, 1, 0, 1, 2, 3, 2, \; 0, 1, 0, 1, 2, 3, 2, \; \ldots

Period seven. The seven is not the sum of anything obvious, not one more than the largest element, and not derivable from the set by any rule anybody has found. It is a fact discovered by running the recursion.

Try {1,2,4}\{1, 2, 4\} and the period is three, with sequence 0,1,2,0,1,2,0, 1, 2, 0, 1, 2, \ldots — the element four is never useful, because subtracting four and subtracting one land on the same residue. Try {2,3}\{2, 3\} and the period is five: 0,0,1,1,2,0,0,1,1,2,0, 0, 1, 1, 2, 0, 0, 1, 1, 2, \ldots, with the two leading zeros reflecting that heaps of nought and one are both dead.

Grundy values for subtraction of 2, 3. The Grundy value of every heap size for a take-away game, computed by the mex rule. A period, if the figure marks one, was found by searching the computed sequence rather than assumed — and where no period is marked, none was found in the range drawn, which is not the same as there being none.
Fig. 3 A set with no small move. Two heap sizes are unplayable, so the sequence opens with a pair of zeros, and the period is five for reasons visible only after the fact.

Three sets of similar shape, three unrelated periods. There is no formula, and the reason is not that nobody has looked.

Why periodicity is the whole question

If the sequence is eventually periodic, the game is solved in a strong sense: a finite table plus a modular arithmetic answers any question about any heap size, however large.

If it is not, the only known way to evaluate a heap of size nn is to compute every value below nn, which is linear in nn and therefore exponential in the size of the input, since a heap size is written down in logn\log n digits. The difference between a periodic sequence and an aperiodic one is the difference between a solved game and one that can only be simulated.

So “is this sequence eventually periodic” is not an aesthetic question about patterns. It is the question of whether the game has a solution at all in the usual sense.

The one theorem there is

For finite subtraction sets, the answer is known and it is good news.

If SS is finite with largest element mm, the Grundy sequence is eventually periodic. The proof is a counting argument: the Grundy value of a heap depends only on the previous mm values, and no Grundy value can exceed S|S|, so there are at most (S+1)m(|S|+1)^m possible windows of mm consecutive values. The sequence of windows must eventually repeat, and once a window repeats the entire subsequent sequence repeats.

That is a pigeonhole argument, and it is worth noticing what it does and does not give. It gives eventual periodicity, guaranteed. It gives a bound on when periodicity starts and how long the period is — but the bound is (S+1)m(|S|+1)^m, which for a set with largest element twenty is astronomically larger than any period ever observed. The theorem says the sequence repeats and is useless for predicting when.

Every actual period is found by computing until it appears.

Octal games, where it gets harder

Subtraction games are the easy family because a move always leaves one heap. Loosen that and a move may split a heap in two, and the analysis changes character.

The standard notation for these is an octal code. In the game 0.07\mathbf{0.07}, the digit seven at position two means: from a heap, remove exactly two counters, and the remainder may be left whole, left as two non-empty heaps, or — the third bit — removed entirely. Kayles is 0.77\mathbf{0.77}; Dawson’s chess is 0.137\mathbf{0.137}.

Splitting means positions are sums, and a heap’s value is the mex over options that are themselves nim-sums of two heaps’ values. The recursion is still mechanical and the sequences become dramatically more erratic.

Kayles, 0.77\mathbf{0.77}, has period twelve — but only from heap size seventy-one onward, with an irregular prefix of seventy-one terms. Nothing about the rules suggests seventy-one. Dawson’s chess has period thirty-four, starting at heap size fifty-two, with a handful of exceptional values scattered through the prefix.

Every entry in either sequence is a single mex over the values of the options, which is the operation the theorem licenses and the only one these strips ever perform. The difficulty is not in any one of them. It is in how many are needed before the answer starts to repeat, and in the fact that nothing in the rule says how many that will be — which is a thing a strip can be drawn to demonstrate against itself.

Grundy values for octal game ·77. The Grundy value of every heap size for a take-away game, computed by the mex rule. A period, if the figure marks one, was found by searching the computed sequence rather than assumed — and where no period is marked, none was found in the range drawn, which is not the same as there being none.
Fig. 4 Kayles over its first twenty-five heaps. Nothing here repeats and nothing here is going to: the period of twelve begins at heap seventy-one, and the figure knows that because it searched all 2,001 values it computed rather than the two dozen it drew. What is on the page is the prefix, and the prefix is the part nobody can explain.

Reading a strip

The strips on this page repay a slower look, because they contain more than the period.

The values that appear are bounded by the number of moves available, and usually well below it. For {1,3,4}\{1,3,4\} the values never exceed three, though the set has three elements and a heap can have many options. Large Grundy values are rare, and a game whose sequence reaches eight — as Kayles does — is unusual enough to be remarked on in the literature.

The zeros are the losing positions, and their spacing is the practical content of the sequence. For {1,3,4}\{1,3,4\} the zeros are at 0,2,7,9,14,16,0, 2, 7, 9, 14, 16, \ldots: a pattern of gaps two and five alternating. A player who memorised only that would play the single-heap game perfectly and would still be helpless at a two-heap position, which needs the whole sequence and an exclusive-or.

The prefix, where there is one, is the part that resists explanation. For {1,3,4}\{1,3,4\}, {1,2,3}\{1,2,3\} and {2,3}\{2,3\} there is none — periodicity begins at heap zero — and three sets in a row behaving that way invite the reading that a subtraction game simply has no prefix. It is not true, and one more element is enough to break it.

Grundy values for subtraction of 2, 4, 7. The Grundy value of every heap size for a take-away game, computed by the mex rule. A period, if the figure marks one, was found by searching the computed sequence rather than assumed — and where no period is marked, none was found in the range drawn, which is not the same as there being none.
Fig. 5 Take two, four or seven. The sequence opens 0, 0, 1, 1, 2, 2, 0, 3 and then falls into 1, 0, 2 repeating for ever, so the period is the shortest on this page and eight heaps sit outside it. Those eight are a prefix in the same sense Kayles has one, at a scale a reader can hold in view: the value three occurs once, at a heap of seven, and never again anywhere in the sequence.

Kayles’ prefix is seventy-one terms, and it is where the rare values live — though not in the way that phrase usually means. The value five occurs exactly once in the entire game, at a heap of twenty-eight. Six occurs four times, at eleven, twenty-two, thirty-four and seventy, and never again. Seven and eight are not exceptional at all: the repeating block is 7, 4, 1, 2, 8, 1, 4, 7, 2, 1, 8, 2, so each of them arrives twice in every period for ever. What the prefix actually contains is the fourteen heaps whose value differs from the periodic continuation, and they are scattered — 0, 3, 6, 9, 11, 15, 18, 21, 22, 28, 34, 39, 57, 70 — with the last of them one heap short of where the period starts.

One more thing is visible in Kayles and in nothing else on this page. Its only zero is the empty heap. Every positive heap of a single Kayles row is a win for whoever moves, so the sequence’s practical content is entirely in the sums: a player facing one row has nothing to decide and a player facing three has the whole problem.

What a period does not buy

Suppose the period is known. What follows?

The one-heap game is solved: reduce the heap size modulo the period, look up the value, and if it is nonzero find the move that reaches zero. That is genuinely a solution and it is fast.

The many-heap game is also solved, by exclusive-or of the components, and this is where the periodicity pays off. Ten heaps of arbitrary size collapse to ten table lookups and nine exclusive-ors.

What is not solved is anything about the structure of play. The sequence says which positions are lost; it does not say how long the game will run, whether the winner has one winning move or many, or how badly a mistake costs. A player handed the sequence plays perfectly and understands nothing, which is a recurring theme — a value is not a strategy, and a table of values is not an account of a game.

Nor does a period explain itself. Knowing that {1,3,4}\{1,3,4\} has period seven does not indicate why, and does not help with {1,3,5}\{1,3,5\}. Each set is its own computation.

Splitting, and why it changes everything

The gap between subtraction games and octal games deserves isolating, because it is where a tractable family becomes an open problem.

In a subtraction game, a move takes a heap of size nn to a heap of size nsn - s. One heap in, one heap out, so the recursion at nn looks back at a fixed window of S|S| earlier values, and the window has bounded width. That bounded width is exactly what the pigeonhole proof needs.

In an octal game with splitting, a move can take a heap of size nn to two heaps of sizes aa and bb with a+b=nsa + b = n - s. The recursion at nn now looks at G(a)G(b)\mathcal{G}(a) \oplus \mathcal{G}(b) over every such pair — a set of options whose size grows with nn, drawing on the entire earlier sequence rather than a fixed window.

There is no bounded state to apply pigeonhole to. The proof does not merely become harder; the object it was about no longer exists. That single structural change is the whole reason Guy’s conjecture is a conjecture, and it is why a game as simple as splitting a heap into two unequal parts has resisted since 1939.

What arrives in place of the window is the exclusive-or. A split leaves two heaps, two heaps are an independent sum, and the option’s value is therefore G(a)G(b)\mathcal{G}(a) \oplus \mathcal{G}(b) — so a single unusual value early in the sequence reaches forward into arbitrarily distant options rather than washing out of a fixed window.

It does not follow that every splitting game is hard, and assuming it would be the wrong lesson. Two of them have answers as short as anything in the subtraction family.

Three take-and-break games, three kinds of answer. The Grundy sequences of Nim, Lasker's Nim and Kayles over the first heaps. Adding a move that removes nothing takes Nim's sequence from the identity to a four-line formula; bounding how much may be taken instead takes it somewhere with no formula at all.
Fig. 6 Three take-and-break games side by side. Nim’s values are the heap size. Lasker’s Nim adds a move that removes nothing and splits a heap in two, and its values are still a four-line rule on the heap size modulo four, exact on every one of the 2,001 heaps the figure checks. Kayles bounds the take instead, and has nothing of the kind — a period of twelve arriving at heap seventy-one with fourteen heaps outside it, which is a solution but not a formula.

So splitting is not what makes a game hard; it is what removes the argument that a game must be easy. The three sequences above cost the same to compute and differ only in whether the answer has a short description, and no property of the rule table announces which of the three a new code will turn out to be.

The open problems

Here is the part that is genuinely unresolved.

Is every finite octal game eventually periodic? Guy conjectured yes in the 1950s. It is open. No finite octal game has been proved aperiodic, and none has been proved to have no period.

0.007\mathbf{0.007} — Grundy values computed past 2402^{40} heap sizes, no period found. Every known term has been computed and the sequence has not repeated.

0.106\mathbf{0.106} — the same story, and it is the smallest code for which nobody knows.

Grundy’s game, where a move splits a heap into two unequal heaps: values computed past 2352^{35}, no period. This one has the simplest rule statement of any unsolved impartial game, and it can be explained to a child in one sentence.

These are not deep-looking problems. The rules fit on a line, the recursion is a first exercise in dynamic programming, and after seventy years of computation and several people’s careers there is no answer. The recursion is trivially computable and the pattern in what it computes is not.

That gap — between a value that can be computed and a value that can be predicted — is the characteristic difficulty of this corner of the subject and a preview of where the theory runs out generally.

What the solver computed

Every strip on this page was generated by running the mex recursion, not by transcribing a published table. One routine turns a rule — a subtraction set, or an octal code — into a sequence of values by taking a mex at each heap in turn. A second looks for the smallest period pp and offset kk such that G(n+p)=G(n)\mathcal{G}(n+p) = \mathcal{G}(n) for every nkn \ge k in the range computed, and reports both.

The second routine has never been told the rule. It is handed a list of integers and nothing else, so it cannot be talked into the period the rule “ought” to have, and it has no way of preferring a short answer to a long one. That is what makes the annotation under each strip a finding rather than a restatement of the caption above it.

It is also why the range is stated. Each strip searches two thousand values and draws twenty-five of them, and the caption quotes the range because a period found within twenty-four terms is a period observed, not a period proved. For the octal games above, the difference between those two words has consumed decades.

Where a claim is stronger than “no disagreement was found”, it is checked against a second computation. The last figure is the case: Lasker’s Nim has a four-clause closed form on the heap size modulo four, and the figure runs the mex loop and the formula against each other on every heap up to two thousand rather than displaying the formula and inviting belief.

For S={1,3,4}S = \{1, 3, 4\} the search reports period seven starting at offset zero. For S={2,3}S = \{2, 3\}, period five at offset zero. For S={1,2,3}S = \{1, 2, 3\}, period four at offset zero. For S={2,4,7}S = \{2, 4, 7\}, period three starting at offset eight — the one set on this page whose period does not begin at the empty heap. Those are the numbers in the captions, and they came out of the code.

Sparse and infinite sets

Two variations are worth mentioning because they break the theorem in different ways.

Infinite subtraction sets. If SS is all the powers of two, the sequence is 0,1,2,0,1,2,0,1,2,0,1,2,\ldots with period three — periodic despite the set being infinite, because only the low bits ever matter. If SS is all the primes, the sequence is far less regular and the pigeonhole proof does not apply at all, since there is no largest element to bound the window.

All-but-finitely-many sets. If SS is every positive integer except one, the game is nearly Nim and the sequence is nearly the identity, with a defect where the missing move would have been. These are the cases where the sequence can be reasoned about rather than computed, and they are a small island.

The general picture is that periodicity is common, proved only in the easiest family, and conjectured everywhere else on the strength of computation.

Who found it, and when

Grundy computed sequences by hand in the 1930s. Richard Guy and Cedric Smith systematised the octal notation and the periodicity questions in The G-values of various games, published in 1956 — a paper that is essentially the founding document of this area, and that already contains the conjecture and the unsolved cases.

Guy returned to the problem repeatedly for the next fifty years and offered a prize for a decision on 0.007\mathbf{0.007}. Achim Flammenkamp maintained the computational record from the 1990s, pushing the search bounds for the unsolved codes by many orders of magnitude and finding nothing.

The list of unsolved octal games in Winning Ways is short and has barely changed since 1982. What has changed is the amount of computation thrown at it, by a factor of billions, with no effect on the answer.

What a sequence is evidence about

A Grundy sequence is a strip of small numbers and it invites being read as a picture, so it is worth saying what a reader is entitled to conclude from looking at one.

A period seen is not a period. A repetition over a window is a repetition over that window, and the whole content of a period is a proof is that a checked window of the right width is a theorem while an observed one is not. The width is not decorative: it is the reach of the move rule, and one value short of it the induction has nothing to stand on.

A period not seen is not the absence of one. A search over periods up to some length, in a window of some size, has looked at what it looked at. The famous open cases in this family are famous because enormous windows have been searched and neither answer has arrived.

And a sequence that never repeats may still be settled. A period with a constant added is a completely solved game whose values climb for ever and use infinitely many symbols, so the alphabet keeps growing proves non-periodicity and says nothing about whether the game is understood.

So the three readings a strip invites — it repeats, it does not repeat, it is a mess — are three different claims with three different evidential standards, and only the first can ever be settled by a finite picture. That is an unusual situation for a figure to be in, and it is why every strip on this site prints how many values were examined rather than merely showing them.

Where the model stops

Heap games only. Subtraction and octal games are played on heaps, which is what makes a one-dimensional sequence the right object. Impartial games on graphs or boards have no such indexing and no such sequences.

Impartial only, as the theorem requires. Partizan games have no Grundy values, and their analogue — the sequence of canonical forms along a family — is far less tractable.

Normal play only. Under misère play these sequences do not describe the game, and the misère analysis of even simple octal games is disproportionately harder.

Observed periods are not proved periods. The figures show what a finite search found. For finite subtraction sets a proof exists, so the observation is confirmation. For octal games no proof exists, and a figure that claimed one would be overstating what the code did.

Where the ladder goes next

grundy-sequences has two rungs to here: what a Grundy sequence is, and what its shape tells a reader.

The rung above changes what is being enumerated, and it is the change that made the whole subject searchable. Octal games compresses a take-and-break rule into a string of digits, so which game becomes something a program can count through rather than something a person has to invent. The family was swept that way in 1956, and the games nobody can solve were found by sweeping it rather than by anybody choosing them.

That is worth reading as a fact about method as much as about games. A sequence is an object to stare at; a rule table is an object to enumerate. Turning the first into the second is what converts look at this sequence and see whether it repeats into sweep every rule and report which repeat, and every open problem in the family — the conjecture that all of them settle, the code that has resisted billions of terms — is a question that could only be posed once the family had a coordinate system.

The other direction is the one that stays open. A sequence’s shape is a description of one game, and the two rungs above this one look for classes: which rules give a period, which give a period with a constant added, which give nothing anybody can find. None of those questions has an answer stated in the digits, and the reason is the standing complaint of this family — Kayles is 77\cdot 77 and has no formula, Lasker’s Nim has one, and nothing about the digits says which is which.

Part 2 of 2

One argument about Grundy sequences. The parts either side of it:

What links here

Essays that reach for this one mid-argument — the half of a link its own author cannot write down, the 8 sharing most with it of 22.

What this makes readable

Essays that declare this one a prerequisite.

The objects named here

The third axis, after the field and the series: the games, values and theorems themselves, and every essay that touches each one.

Grundy valueImpartialMexNimOctal gamePeriodicitySubtraction game