taocp

This site has no text of the book. Keep your own copy of TAOCP next to it. See what is checked.

Exercise · Volume 1 · 1.2.8-38

Exercise 1.2.8-38

not stated yet in Lean solution written

Rating35
KindK6, write a program: write a MIX or MMIX program from the written solution
SectionSection 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 (n,q)(n, q): if n≤qn \le q, take all nn chips. Otherwise let μ\mu be the smallest term of the Fibonacci representation of nn. If μ≤q\mu \le q, take μ\mu chips, which wins by Exercise 1.2.8-37. If μ>q\mu > q the position is lost, and the program takes 11 chip, which keeps the game going and gives the opponent a chance to err.

The first move with nn chips is the same rule with q=n−1q = n - 1.

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 1,2,3,5,8,…1, 2, 3, 5, 8, \ldots (without the repeated 11). 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 O(log⁡n)O(\log n) steps.

Correctness. If n≤qn \le q the move wins at once. If μ≤q\mu \le q the move wins by the winning side of the claim of Exercise 1.2.8-37. If μ>q\mu > q 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 n≥1n \ge 1 and q≥1q \ge 1.

Check

first_move(1000) returns 1313. The first moves for n=2,3,4,5,6,7,8,12,20,100n = 2, 3, 4, 5, 6, 7, 8, 12, 20, 100 are 1,1,1,1,1,2,1,1,2,31, 1, 1, 1, 1, 2, 1, 1, 2, 3 (for the Fibonacci numbers n=2,3,5,8n = 2, 3, 5, 8 the position is lost and the move 11 is only a delay). A python test against a complete game-tree search for all piles n<160n < 160 and all limits q≤nq \le n finds that choose always returns a legal move, and a winning move whenever one exists. The program also gives the first move 22 for n=1018n = 10^{18}.

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.