The vile, dopy, evil and odious game players

Aviezri S. Fraenkel∗ Department of Computer Science and Applied Mathematics Weizmann Institute of Science Rehovot 76100, Israel February 7, 2010

Wil: We have worked and published jointly, for example on graph focality. Now that one of us is already an octogenarian and the other is only a decade away, let’s have some fun; let’s play a game. Gert: I’m game. Wil: In the Fall 2009 issue of the MSRI gazette Emissary, and Joe Buhler proposed the following puzzle: “Nathan and Peter are playing a game. Nathan always goes first. The players take turns changing a positive integer to a smaller one and then passing the smaller number back to their op- ponent. On each move, a player may either subtract one from the integer or halve it, rounding down if necessary. Thus, from 28 the legal moves are to 27 or to 14; from 27, the legal moves are to 26 or to 13. The game ends when the integer reaches 0. The player who makes the last move wins. For example, if the starting integer is 15, a legal sequence of moves might be to 7, then 6, then 3, then 2, then 1, and then to 0. (In this sample game one of the players could have played better!) Assuming both Nathan and Peter play according to the best possible strategy, who will win if the starting integer is 1000? 2000?” Let’s dub it the Mark game, since it’s due to Mark Krusemeyer according to Berlekamp and Buhler. Gert: To get a feel for the Mark game, I’d construct a small table listing its P -positions (P revious player wins) and N-positions (Next player wins). For example, 0 ∈ P, since the Next (first) player cannot move, so the Previous (second) player wins by default, 1 ∈ N since Next can move to 0 ∈ P; and 2 ∈ P. In general, every position that has a follower in P is in N , and every position all of whose followers are in N is in P.(P and N are the set of all P - and N-positions respectively.)

[email protected] http://www.wisdom.weizmann.ac.il/~fraenkel

1 Wil: (Extracting his palm computer)... Here it is!

n 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

P/N PNPNNNPNPNPNNNPNNNPNNNPN

Gert: It’s hard to see what’s going on...It might be useful to separate out the P -positions and the N-positions into two sequences.

Wil: Alright, the rearranged table below suggests that bn = 2an for every nonnegative integer n, P = ∪n≥0bn, N = ∪n≥1an, where an = Nn, n ≥ 1; bn = Pn, n ≥ 0. But what’s Nn?... Oh I see, Nn = mex{,Ni : 0 ≤ i < n} for every n ≥ 0, where mex of a finite subset of nonnegative integers is the least nonnegative integer not in the set. In particular, mex of the empty set is 0. Notice that the sequences (for n ≥ 1) are complementary: they split the positive integers

n 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

an 0 1 3 4 5 7 9 11 12 13 15 16 17 19 20 21 23 25 27 28 29 31 33 35

bn 0 2 6 8 10 14 18 22 24 26 30 32 34 38 40 42 46 50 54 56 58 62 66 70

Gert: Since 15 ∈ N which has the follower 14 ∈ P, we indeed see, as hinted by Berlekamp-Buhler, that Nathan could have played better by moving 15 → 14 rather than 15 → 7, thus securing his win! But for deciding 1000 and 2000, the recursive definition of the P - and N-positions is not too convenient. Is there a “closed form” formula for them, I wonder? Wil: Well, the second sequence is not a “spectrum”, i.e., there exist no real α, γ such that bn = ∪n≥0bnα + γc (bxc is the integer part of the real x), since a necessary – though not sufficient – condition for that is that bn+1−bn ∈ {k, k+1} for some integer k, and here the differences are 2 and 4. Since the sequences are complementary, also the first sequence is not a spectrum... However, the fact that there are two followers and one of them is halving, suggests to consider some sort of binary numeration system. Gert: The simplest such system is the ordinary positional binary system... Indeed, it appears that P is the set of all dopy numbers and N is the set of all vile numbers.

2 Wil: What are dopy and vile numbers? Gert: The dopy numbers are those whose binary representations end in an odd number of 0s, and the vile numbers are those that end in an even number of 0s. Wil: ...No doubt their names are inspired by the odious and evil numbers, those that have an odd and an even number of 1’s in their binary represen- tation respectively. To indicate that we count 0s rather than 1s, and only at the tail end, the “od” and “ev” are reversed to “do” and “ve” in “dopy” and “vile”. “Odious” and “evil” where coined by Elwyn Berlekamp, John Conway and Richard Guy while composing their famous book Winning Ways.

Gert: Precisely. Indeed, the sequence {an}n≥1 consists of all alternately evil and odious numbers: a2n−1 odious, a2n is evil (n ≥ 1); the same holds for bn, which is just a shift of an... The binary representations of 1000 and 2000 are 1111101000 and 11111010000 respectively, so Peter can win 1000 and Nathan can win 2000. This solves the puzzle for every positive integer k with a linear-time algorithm in its input size O(log k). Anything else?

Wil: Yes, but before that my palm notices that the sequences {an}n≥1 and {bn}n≥1 are the sequences A003159 and A036554 respectively in the famous Neslopedia. Gert: Neslopedia?... Oh, you mean ’s Encyclopedia of Integer Sequences, indeed a wonderful tool for doing math, especially discrete math. Wil: Yes! As to your question “anything else?”, it would be nice to play this game in a sum of games. Gert: You mean, given a finite collection of games, Nathan and Peter each select a game at each of their turns and make a legal move therein? The player making the last move in the last surviving game wins? Wil: Yes on both counts... I think that the P , N tool is not strong enough to decide sums of games. Gert: ...Right. For example, the sum of two Mark games with value 1 ∈ N is clearly a P -position in their sum, yet the sum of 1 and 2 ∈ N is an N-position in their sum. Indeed, the first player can move to (1, 1) and win. However, the sum of P -positions is always a P -position in the sum. Wil: To analyze sums, it’s helpful to compute the Sprague-Grundy function, g-function for short, and then compute the Nim-sum (or XOR, or sum over GF(2)) of the g-values: Nim-sum 0 is equivalent to a P -position of a single game; nonzero Nim-sum – to an N-position of a single game... My palm computer

3 produced the following table.

n 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

g 010212010201210212012102

Gert: Notice that there can be no value larger than 2, since each game position has only two followers. All the 0s of g (beyond 0) clearly comprise all the dopy numbers — the P -positions. But which are 1s and which are 2s?... Oh, the odious and evil numbers raise their revolting heads again! All the 1s of g comprise all the vile-odious numbers and all the 2s of g comprise all the vile-evil numbers. Wil: So if I leave you in the 3-game position (1, 3, 4) where will you move to? Gert: Well, 1 + 3 + 4 = 8, so I see that you left me in a P -position from which, whatever I do, I’ll lose. I expected a more gentlemanly gesture from you. Wil: You are indeed vile and evil! Gert: I think that we won’t be friends anymore after all these unwarranted insults! Wil: All I meant is that the position I suggested to you is vile-evil: g(1) ⊕ g(3) ⊕ g(4) = 1 ⊕ 2 ⊕ 1 = 2, and g(n) = 2, is equivalent to a single game where n is vile-evil, as you had pointed out previously. I suggested to you a position from which you can win, namely, with the move 3 → 2. Gert: Indeed, this move makes the position dopy. I apologize profusely. I erred twice: Instead of Nim-summing the g-values, I summed their arguments... The Neslopedia indicates that {n : g(n) = 1} seems to comprise all vile-odious numbers, and {n : g(n) = 2} comprises all vile-evil numbers... Incidentally, until now we considered normal games, that is, the player mak- ing the last move wins. What about mis`ere play of Mark, where the player making the last move loses? Wil: Then we can assume that 1 is the last move, so 1 ∈ P. This, with the help of my palm, produces the following results, where an = Nn and bn = Pn, n ≥ 0.

4 n 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

an 0 2 3 5 7 8 9 11 12 13 15 17 19 20 21 23 25 27 28 29 31 32 33 35

bn 1 4 6 10 14 16 18 22 24 26 30 34 38 40 42 46 50 54 56 58 62 64 66 70

Gert: So for mis`ereplay of Mark, we seem to have an = mex{ai, bi : 0 ≤ i < n} for n ≥ 0, b0 = 1 and bn = 2an for n ≥ 1. Is there again a “digital” characterization?... Yes, it appears that P of Mark’s mis`ereplay is the set of all dopy numbers except that all dopy powers of 2 are replaced by all vile powers of 2; and N is the set of all vile numbers except that all vile powers of 2 are replaced by all dopy powers of 2. This provides a linear algorithm in the input size O(log k) of k for deciding whether k ∈ P or k ∈ N for mis`ereplay of Mark. Wil: These two sequence are not yet in the Neslopedia, but, as you point out, the {an}n≥1-sequence is the same as A003159 except for the interchange of vile by dopy powers of 2, and {bn}n≥1 is the same as A036554 except for the interchange of dopy by vile powers of 2... We could compute the g-function for this game, but it would only enable us to play sums of Mark’s game, where the end position of each component game is 1. For the sum of Mark’s mis`ere plays, the end position of every component game is 0, except for the one played last, whose end position is 1. What do you think? Gert: I agree, and we better leave the mis`eresum analysis to the famous mis`eregurus Thane Plambeck and Aaron Siegel, though the P -positions of normal and mis`ereagree except that the powers of 2 are swapped... We might, however, look at the game Mark-2, which is the same as Mark, except that when halving, we round up rather than down. For example, the only follower of 3 is 2. Wil: ...In normal play, where the player making the last move to 0 wins, the position 1 has followers 0 and 1 in Mark-2. The game is thus loopy, and it’s the generalized Sprague-Grundy function γ that’s needed. In fact, γ(1) = ∞(0), and the sum of two Mark-2 games with position (1, 1) is clearly a draw. Gert: Alright, so for the time being, let’s consider the version where 1 is the end-position. The player first reaching 1 wins... My PC produced the following

5 table for Mark-2, where an = Nn for n ≥ 1 and bn = Pn for n ≥ 0.

n 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

an 0 2 4 5 6 8 10 12 13 14 16 17 18 20 21 22 24 26 28 29 30 32 34 36

bn 1 3 7 9 11 15 19 23 25 27 31 33 35 39 41 43 47 51 55 57 59 63 67 71

Wil: It appears that an = mex{ai, bi : 0 ≤ i < n} for n ≥ 0, b0 = 1 and bn = 2an − 1 for n ≥ 1... It further seems that the P -positions are all odd numbers that are alternately odious and evil, greedily chosen, beginning with 1, which is odious. Define the spite of k to be evil if k is evil; odious if k is odious. Then “greedily chosen” means the following: 1 ∈ P, and if we have already shown that 2k − 1 ∈ P, then 2k + 1 ∈ P if 2k − 1 and 2k + 1 are of opposite spite-parity. Otherwise 2k + 3 ∈ P... Incidentally, neither the an- nor the bn-sequence is in the Neslopedia.

Gert: The set of excluded odd numbers from {bn} is

S0 = 5, 13, 17, 21, 29, 37, 45, 49, 53, 61, 65, 69,..., which is not in the Neslopedia either. But the set of bn-numbers just before the excluded ones, namely

S− = 3, 11, 15, 19, 27, 35, 43, 47, 51, 59, 63, 67,... is A131323 in the Neslopedia. On the other hand, the set of those just after, namely S+ = 7, 15, 19, 23, 31, 39, 47, 51, 55, 63, 71 ... is not in. Thus S0 = S− + 2 =A131323+2 = S+ − 2. Wil: Is there a concise characterization of P for Mark-2, I wonder?... Given a positive odd integer k in binary expansion, we examine its spite. If k and k − 2 have opposite spite-parity, then k ∈ P. Otherwise, k − 2 ∈ P and k ∈ N . This constitutes a linear algorithm in the input size O(log k) of k for determining whether or not k ∈ P.

Gert: Notice that S0 appears to consist of precisely all alternately evil and odious numbers whose binary representation ends in 02n+11 for any n ≥ 0, i.e., a prefix of an odd number of 0s followed by 1. And P (beyond 0) seems to consist of precisely all alternately evil and odious numbers whose binary representation ends in 02n1 for any n ≥ 0. This provides an even simpler linear algorithm. Wil: Yes. Now consider the variation...

6 Gert: I admit that we really had fun, but I’m getting rather famished... Wil: But what about proofs? Gert: We might as well leave that to the younger generation... for example to Aviezri. I was born October 28, 1929, and I think that he was born much later in 1929! Wil: So we’ll have to wait a zillion years till we see proofs! Gert: Don’t be a smarty. You know that I meant an exclamation mark, not a . – Let’s now go to have dinner. Wil: All I meant is that it will take that guy a zillion years to write up the proofs.

7