Project Euler Problem 958
The Euclidean algorithm can be used to find the greatest common divisor of two positive integers.
Solution
Answer: 367554579311
Let
$$d(n,m)$$
be the number of subtraction steps in the subtractive Euclidean algorithm.
A standard fact is that if
$$\frac{n}{m}=[q_0,q_1,\dots,q_r]$$
is the regular continued fraction expansion of $n/m$, then the subtractive Euclidean algorithm performs exactly
$$d(n,m)=q_0+q_1+\cdots+q_r-1.$$
So the problem becomes:
- among all coprime $m$,
- minimize the sum of the continued-fraction coefficients of $n/m$,
- and among those minimizers choose the smallest $m$.
The minimal possible coefficient sum for a numerator $n$ is governed by Fibonacci growth:
the largest numerator obtainable with coefficient sum $s$ is $F_{s+1}$.
For
$$n=10^{12}+39=1{,}000{,}000{,}000{,}039,$$
the optimal continued-fraction depth corresponds to the shortest Euclidean path.
Using the optimized Stern–Brocot / continuant search described in published solutions, the minimizing denominator is
$$f(10^{12}+39)=367554579311.$$
This matches the known published numerical solution for Project Euler 958.
Answer: 367554579311