Number Theory Games - Lecture 10 · 10/12/2018 · Justin Stevens Number Theory Games (Lecture 10)...
Transcript of Number Theory Games - Lecture 10 · 10/12/2018 · Justin Stevens Number Theory Games (Lecture 10)...
Number Theory GamesLecture 10
Justin Stevens
Justin Stevens Number Theory Games (Lecture 10) 1 / 30
Outline
1 Number Theory GamesTAG5Divisors of 60 GameNimFibonacci Nim
2 Euclidean Algorithm Revisited
3 Parting Shots
Justin Stevens Number Theory Games (Lecture 10) 2 / 30
TAG5 Rules
Consider the take-away game TAG5 below:Let there be n chips on the table and A,B be two players, Amaking the first move. Each player may in turn take-away either1, 2, 3, 4, or 5 chips from the pile. The winner is the player whoremoves the final chip(s), leaving none for the other player.
Play TAG5 for n = 42. Who has the winning strategy?Play TAG5 for n = 50. Who has the winning strategy?For a general n, does player A or player B have the winning strategy?
Justin Stevens Number Theory Games (Lecture 10) 3 / 30
TAG5 Analysis
12
9 10 1187
6
3 4 521
0
Justin Stevens Number Theory Games (Lecture 10) 4 / 30
Reducing by Divisors Game
Consider the variation of the take-away game below:Let there be 60 chips on the table and A,B be two players, Amaking the first move. Each player in turn generates newnumbers by subtracting a positive proper divisor from the currentsum. The winner is the player who makes the new number 1.
Does player A or player B have the winning strategy?
Justin Stevens Number Theory Games (Lecture 10) 5 / 30
Divisors Game Analysis
23456789
Justin Stevens Number Theory Games (Lecture 10) 6 / 30
Nim
Consider the rules for the following game, known as Nim:There are four piles of stones of size 1, 3, 5, and 7. Players A andB alternate choosing a pile and taking at least one stone fromthat pile. The game continues until there are no stones remaining.
There are two variations on the winning condition:Normal Play: The player to take the last stone wins.Misère Play: The player to take the last stone loses.
Justin Stevens Number Theory Games (Lecture 10) 7 / 30
Nim-Sum Definition
Definition. The nim-sum of two numbers a and b is denoted a ⊕ b. Toform this, write the two numbers in binary and add without carry:
0⊕ 0 = 1⊕ 1 = 0 and 0⊕ 1 = 1⊕ 0 = 1.
The operation is also known as “exclusive or" (xor).For piles of size 1, 3, 5, and 7, consider the table below:
22 21 20
1 0 0 13 0 1 15 1 0 17 1 1 1
0 0 0
Therefore, 1 ⊕ 3 ⊕ 5 ⊕ 7 = 0. Whenthe nim-sum is 0, we call a positionbalanced. Otherwise, it is unbalanced.
Example. Compute 5⊕ 6⊕ 7 and 9⊕ 10⊕ 15.
Justin Stevens Number Theory Games (Lecture 10) 8 / 30
Nim-Sum Examples
Example. Compute 5⊕ 6⊕ 7 and 9⊕ 10⊕ 15.
22 21 20
5 1 0 16 1 1 07 1 1 1
1 0 0
Therefore, 5⊕6⊕7 = 4. To make the positionbalanced, we remove 4 from any pile. Then,
1⊕ 6⊕ 7 = 5⊕ 2⊕ 7 = 5⊕ 6⊕ 3 = 0.
23 22 21 20
9 1 0 0 110 1 0 1 015 1 1 1 1
1 1 0 0
Therefore, 9 ⊕ 10 ⊕ 15 = 10. To make theposition balanced, we remove 4 from one ofthe first two piles, or 12 from the last pile:5⊕ 10⊕ 15 = 9⊕ 6⊕ 15 = 9⊕ 10⊕ 3 = 0.
Justin Stevens Number Theory Games (Lecture 10) 9 / 30
Winning Strategy
The practical strategy to win at the game of Nim is for a player to get theother into one of the following positions:
2 Heaps 3 Heaps 4 Heaps2 2 1 2 3 1 1 n n3 3 1 4 5 1 2 4 74 4 1 6 7 1 2 5 65 5 1 8 9 1 3 4 66 6 2 4 6 1 3 5 77 7 2 5 7 2 3 4 5
For Misére Nim, only the last move changes. In particular,Play exactly like you would in normal play until your opponent leavesone pile of size greater than one.At this point, reduce this pile to size 1 or 0, whichever leaves an oddnumber of piles with only one object.
Justin Stevens Number Theory Games (Lecture 10) 10 / 30
Misére Nim Example
Figure 1: Mathologer
Justin Stevens Number Theory Games (Lecture 10) 11 / 30
Proof of Winning Strategy in Nim
Theorem. (Charles Bouton) In a normal Nim game, the player making thefirst move has a winning strategy if and only if the nim-sum of the sizes ofthe heaps is nonzero. Otherwise, the second player has a winning strategy.Proof. Notice that the nim-sum (⊕) obeys the usual associative andcommutative laws of addition and also satisfies the property x ⊕ x = 0.Let x1, · · · , xn be the size of the heaps before a move, and y1, · · · , yn be thecorresponding sizes after. Let s = x1 ⊕ · · · ⊕ xn and t = y1 ⊕ · · · ⊕ yn. Ifthe move was in heap k, we have xi = yi for all i 6= k, and xk > yk . Then,
t = 0⊕ t= s ⊕ s ⊕ t= s ⊕ (x1 ⊕ · · · ⊕ xn)⊕ (y1 ⊕ · · · ⊕ yn)= s ⊕ (x1 ⊕ y1)⊕ · · · ⊕ (xn ⊕ yn)= s ⊕ 0⊕ · · · ⊕ 0⊕ (xk ⊕ yk)⊕ 0⊕ · · · ⊕ 0= s ⊕ xk ⊕ yk .
Justin Stevens Number Theory Games (Lecture 10) 12 / 30
Proof of Winning Strategy in Nim Continued
We’ve proven that if there is a move in heap k, then t = s ⊕ xk ⊕ yk .Lemma. If s = 0, then t 6= 0 no matter what move is made.
Proof.If there are no moves, then the lemma is vacuously true. Otherwise, anymove in heap k produces t = xk ⊕ yk , which is nonzero since xk 6= yk .
Lemma. If s 6= 0, then it is possible to make a move so that t = 0.
Proof.Let d be the position of the leftmost nonzero bit in the binary form of s,and choose k such that the dth bit of xk is also nonzero. Let yk = s ⊕ xk .
Since bit d decreases from 1 to 0 and any change in the remaining bits is atmost 2d − 1, the first player can take xk − yk stones from heap k. Then,
t = s ⊕ xk ⊕ yk = s ⊕ xk ⊕ (s ⊕ xk) = 0.
Justin Stevens Number Theory Games (Lecture 10) 13 / 30
Fibonacci Nim
Consider the rules for the following game, known as Fibonacci Nim:Let there be n chips on the table and A,B be two players whoalternate removing chips from the pile. On the first move, a playeris not allowed to take all of the coins, and on each subsequentmove, the number of coins removed can be any number that is atmost twice the previous move. The winner is the player whoremoves the final chip(s), leaving none for the other player.
Play Fibonacci nim for n = 140. Who has the winning strategy?Play Fibonacci nim for n = 144. Who has the winning strategy?For a general n, does player A or player B have the winning strategy?
Justin Stevens Number Theory Games (Lecture 10) 14 / 30
Zeckendorf’s Theorem
Example. (Zeckendorf’s Theorem) Prove that every positive integer N canbe represented as the unique sum of non-consecutive Fibonacci numbers. Inother words, there exists a unique {aj}mj=0 such that
N =m∑
j=0Faj , a0 ≥ 2 and aj+1 > aj + 1.
Justin Stevens Number Theory Games (Lecture 10) 15 / 30
Fibonacci Sum Existence
Proof by Strong Induction.For the base case N = 1, the unique representation is 1 = F2. Now, assumethat every every integer up to K can be written as the unique sum ofdistinct non-consecutive Fibonacci numbers. Let Fa be the largest Fibonaccinumber such that Fa ≤ K + 1. If Fa = K + 1, then we are clearly done.Otherwise, Fa < K + 1 < Fa+1, therefore
0 < (K + 1)− Fa < Fa+1 − Fa = Fa−1. (?)
By our hypothesis, there exists a sequence {aj}mj=0 with aj+1 > aj + 1 and
K + 1 = Fa +m∑
j=0Faj .
Since m > a + 1 by (?), we have found a representation for K + 1.
Justin Stevens Number Theory Games (Lecture 10) 16 / 30
Fibonacci Sum Uniqueness
LemmaThe sum of any set of distinct, non-consecutive Fibonacci numbers whoselargest member is Fj is strictly less than the next Fibonacci number Fj+1.
For the sake of contradiction, assume we have two representations:
K + 1 = Fa1 + Fa2 + · · ·+ Fam = Fb1 + Fb2 + · · ·+ Fbl .
WLOG assume that am ≥ bl . If am > bl , by our Lemma,
K + 1 = Fb1 + Fb2 + · · ·+ Fbl < Fbl +1 − 1≤ Fam − 1< Fa1 + Fa2 + · · ·+ Fam
= K + 1.
Therefore am = bl . By our hypothesis, K + 1− Fam = K + 1− Fbl has aunique representation, therefore, K + 1 also has a unique representation.
Justin Stevens Number Theory Games (Lecture 10) 17 / 30
Fibonacci Nim Strategy
Theorem. When there is a Fibonacci number of chips in the starting pile,the position is losing for the first player. Otherwise, the first player can win.
Proof. We describe the optimal strategy in Fibonacci nim in terms of the“quota" q (the maximum number of coins that can currently be removed)and the Zeckendorf representation. In particular,
A given position is losing when q is less than the smallest Fibonaccinumber in the representation.In a winning position, it is always a winning move to remove all thecoins (if this is allowed) or otherwise to remove a number of coinsequal to the smallest Fibonacci number in the representation.
When this is possible, the opposing player will necessarily be faced with alosing position, because the new quota will be smaller than the smallestFibonacci number in the Zeckendorf representation of the remaining chips.
Justin Stevens Number Theory Games (Lecture 10) 18 / 30
Sprague-Grundy theorem
Figure 2: In combinatorial game theory, the Sprague-Grundy theorem states thatevery impartial game under the normal play convention is equivalent to a nimber.
Justin Stevens Number Theory Games (Lecture 10) 19 / 30
Outline
1 Number Theory Games
2 Euclidean Algorithm RevisitedThe Game of EuclidMatrices
3 Parting Shots
Justin Stevens Number Theory Games (Lecture 10) 20 / 30
The Game of Euclid
A third variation is a two-player game called The Game of Euclid :Let (p, q) be a pair of positive integers satisfying p > q and letA,B be two players, A making the first move. Each player may inturn subtract as many times the smaller of the remaining integersfrom the larger without making the result negative. The winner isthe player who discovers the highest common factor of p and q,that is, by the Euclidean algorithm, the player who reduces one ofthe factors to zero. [Cole and Davie, 1969]
The game has a winning strategy utilizing the golden ratio, ϕ = 1.618 . . ..
(162, 100) A→ (100, 62) B→ (62, 38) A→ (38, 24) B→ (24, 14) A→ (14, 10) B→ (10, 4) A→ (6, 4) B→ (4, 2) A→ (2, 0)
(161, 100) A→ (100, 61) B→ (61, 39) A→ (39, 22) B→ (22, 17) A→ (17, 5) B→ (7, 5) A→ (5, 2) B→ (3, 2) A→ (2, 1) B→ (1, 0).
A wins the first game, while B wins the second. Observe 1.62 > ϕ > 1.61.
Justin Stevens Number Theory Games (Lecture 10) 21 / 30
Properties of the Game of Euclid
Example. For starting position (n,m), the Game of Euclid has properties:(1) The ending state of the game is (gcd(n,m), 0).(2) For 1 < a/b < ϕ, the only move is (a, b)→ (b, a′), where b/a′ > ϕ.
Proof.(1) For the move (n,m)→ (n − sm,m), by the Euclidean algorithm,gcd(n,m) = gcd(n − sm,m). Therefore, each move preserves the gcd.Furthermore, each move reduces one of the two numbers. Since negativeintegers are not permitted, the ending state of the game is (gcd(n,m), 0).
(2) Since b < a < bϕ < 2b, the only possible move is (a, b)→ (b, a − b).Hence a′ = a − b. Furthermore, ϕ2 − ϕ = 1, so
ba′ = b
a − b = 1a/b − 1 >
1ϕ− 1 = ϕ.
Justin Stevens Number Theory Games (Lecture 10) 22 / 30
Winning Strategy of the Game of Euclid
Theorem. Player A may force a win if n/m = 1 or if n/m > ϕ.
Proof by Infinite Descent.When ϕ < n/m < 2, player A moves to (m, n −m) since
mn −m = 1
n/m − 1 <1
ϕ− 1 = ϕ.
When n/m > 2 and n ≡ r (mod m) for 0 ≤ r < m, there are two moves:
(n,m)→ (m, r) or (n,m)→ (m + r ,m).
If r = 0, player A wins. Otherwise, ϕ is between m/r and (m + r)/m.Player A moves to the position whose ratio lies strictly between 1 and ϕ.
Player B is left in position (a, b) where 1 < a/b < ϕ. Player B must thenmove to (b, a′) where b/a′ > ϕ, from which the process is repeated.
Justin Stevens Number Theory Games (Lecture 10) 23 / 30
2× 2 Matrices
A 2× 2 matrix stores 4 pieces of information: A =[a11 a12a21 a22
].
To add two 2× 2 matrices,
A + B =[a11 a12a21 a22
]+[b11 b12b21 b22
]=[a11 + b11 a12 + b12a21 + b21 a22 + b22
].
To multiply a matrix A by a vector ~x ,
A~x =[a11 a12a21 a22
] [x1x2
]=[a11x1 + a12x2a21x1 + a22x2
].
To multiply two 2× 2 matrices,
AB =[a11 a12a21 a22
] [b11 b12b21 b22
]=[a11b11 + a12b21 a11b12 + a12b22a21b11 + a22b21 a21b12 + a22b22
].
Justin Stevens Number Theory Games (Lecture 10) 24 / 30
Bézout’s Identity Example
Example. Find integers m and n such that 107m + 44n = 1.Solution. The first method uses the Euclidean algorithm in reverse:
107 = 44 · 2 + 19 1 = 7 · 107− 17 · 44 ↑44 = 19 · 2 + 6 1 = −3 · 44 + 7 · 19 ↑19 = 6 · 3 + 1 =⇒ 1 = 19− 3 · 6 ↑
Another method rewrites the first equation as[10744
]=[2 11 0
] [4419
].
Continuing in this manner by looking at the quotients,[10744
]=[2 11 0
] [2 11 0
] [3 11 0
] [6 11 0
] [10
].
Let M represent the product of all the quotient matrices. We compute
M =[107 1744 7
].
Justin Stevens Number Theory Games (Lecture 10) 25 / 30
Inverses and Determinants
The inverse of a 2× 2 matrix A is a matrix A−1 such that
AA−1 = A−1A = I =[1 00 1
].
We can compute an explicit formula for the inverse:[a bc d
]−1
= 1ad − bc
[d −b−c a
].
The determinant of a 2× 2 matrix is defined by
det[a bc d
]=∣∣∣∣∣a bc d
∣∣∣∣∣ = ad − bc.
Determinants are distributive, therefore detAB = detA detB.
Justin Stevens Number Theory Games (Lecture 10) 26 / 30
Matrix Form of Euclidean Algorithm
Theorem. The gcd of two integers a and b can be computed by:
a = q0b + r0b = q1r0 + r1· · ·
rn−2 = qnrn−1 + 0.
We rewrite this as a product of quotient matrices and a remainder vector:[ab
]=[q0 11 0
] [br0
]=[q0 11 0
] [q1 11 0
] [r0r1
]= · · · =
n∏i=0
[qi 11 0
] [rn−10
].
Let M =[m11 m12m21 m22
]represent the product of all the quotient matrices.
Justin Stevens Number Theory Games (Lecture 10) 27 / 30
Matrix Form of Bézout’s Identity
If M =[m11 m12m21 m22
]=
n∏i=0
[qi 11 0
], then the Euclidean algorithm is
[ab
]= M
[rn−10
]= M
[g0
].
To express g as a linear combination of a and b, we must compute M−1.We see detM = (−1)n+1, since it equals the product of the determinants ofthe quotient matrices, each of which is negative one. By the inverse formula,[
g0
]= M−1
[ab
]= (−1)n+1
[m22 −m12−m21 m11
] [ab
].
The top equation is g = (−1)n+1 (m22a −m12b), therefore integersolutions to ax + by = g are x = (−1)n+1 m22 and y = (−1)n m12.
Justin Stevens Number Theory Games (Lecture 10) 28 / 30
Outline
1 Number Theory Games
2 Euclidean Algorithm Revisited
3 Parting Shots
Justin Stevens Number Theory Games (Lecture 10) 29 / 30
Relevant Links
Matt Parker: The Unbeatable Game from the 60s: Dr NIMMathologer: NIM, or, always WIN with mathArchimedes’ Lab: Play Nim against your computer!CL Bouton: Nim, a Game with Complete Mathematical Theory.Steven Vajda: Mathematical Games and How to Play ThemA Game Based on the Euclidean Algorithm and a Winning Strategy for It
Justin Stevens Number Theory Games (Lecture 10) 30 / 30