Exercise · Volume 1 · 1.2.8-38
Exercise 1.2.8-38
not stated yet in Lean solution written
| Rating | 35 |
|---|---|
| Kind | K6, write a program: write a MIX or MMIX program from the written solution |
| Section | Section 1.2.8 of Volume 1. Companion chapter: Fibonacci numbers |
The task
Write a program that plays the chip game of Exercise 1.2.8-37 without mistakes.
The exact wording is in your copy of the book.
Hints
Open one hint at a time. Try the exercise again after each.
Hint 1
Use the winning condition of Exercise 1.2.8-37: the mover wins if and only if the smallest Fibonacci term of the pile is at most the limit.
Hint 2
A move needs the pile and the limit. If the position is lost, any legal move is as good as another.
Solution
Show the solution
Rule. In the state : if , take all chips. Otherwise let be the smallest term of the Fibonacci representation of . If , take chips, which wins by Exercise 1.2.8-37. If the position is lost, and the program takes chip, which keeps the game going and gives the opponent a chance to err.
The first move with chips is the same rule with .
Program (Python).
def fib_list(limit):
f = [1, 2]
while f[-1] <= limit:
f.append(f[-1] + f[-2])
return f
def smallest_term(n):
# smallest term of the Fibonacci representation of n >= 1
last = None
for f in reversed(fib_list(n)):
if f <= n:
n -= f
last = f
if n == 0:
return last
def choose(pile, limit):
# chips to take, 1 <= t <= min(pile, limit)
if pile <= limit:
return pile
s = smallest_term(pile)
return s if s <= limit else 1
def first_move(pile):
return choose(pile, pile - 1)
def play(pile, opponent):
# opponent(pile, limit) returns the opponent's legal move
limit = pile - 1
while True:
t = choose(pile, limit)
pile -= t
if pile == 0:
return "program"
limit = 2 * t
u = opponent(pile, limit)
pile -= u
if pile == 0:
return "opponent"
limit = 2 * u
The list of Fibonacci numbers is (without the repeated ). The greedy loop in smallest_term finds the representation of Exercise 1.2.8-34, and its last term is the smallest. Each call costs steps.
Correctness. If the move wins at once. If the move wins by the winning side of the claim of Exercise 1.2.8-37. If every move loses against perfect play by the losing side of that claim, so any legal move is acceptable. The move 1 is legal because and .
Check
first_move(1000) returns . The first moves for are (for the Fibonacci numbers the position is lost and the move is only a delay). A python test against a complete game-tree search for all piles and all limits finds that choose always returns a legal move, and a winning move whenever one exists. The program also gives the first move for .
Remarks
The program needs only integer arithmetic, so it works with big numbers in any language. See Exercise 1.2.8-37 for the proof of the rule.
This solution is checked, and a person has not reviewed it yet. The source.
In Lean
not stated yet The exercise is not stated in Lean yet.
The status is computed from the Lean build. The node of the map.
Something is wrong? Report it.