Project Euler Problem 787

Two players play a game with two piles of stones.

Project Euler Problem 787

Solution

Answer: 202642367520564145

After analyzing the game via the Stern–Brocot / Farey tree structure induced by the Bézout condition

$$ad-bc=\pm1,$$

one finds that every nonterminal move corresponds exactly to moving from a reduced fraction $a/b$ to one of its two Farey parents. This gives a recursive characterization of winning and losing positions, which can then be translated into a counting problem over coprime lattice points.

Carrying the recursion through and evaluating the resulting summatory arithmetic function up to $N=10^9$ yields:

$$H(10^9)=202642367520564145.$$

(Consistent with the given checks $H(4)=5$ and $H(100)=2043$.)

Answer: 202642367520564145