GCD Sequence
Define the sequence g(n) by g(1) = 1 and g(n) = g(n-1) + gcd(n, g(n-1)) for n >= 2. Find g(10^15).
Problem Statement
This archive keeps the full statement, math, and original media on the page.
Let $g(n)$ be a sequence defined as follows: $$\begin{cases} g(4) = 13 \\ g(n) = g(n - 1) + \gcd\left((n, g(n - 1))\right), \quad \text{for } n > 4 \end{cases}$$ The first few values are:
| $n$ | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | $\ldots$ |
| $g(n)$ | $13$ | $14$ | $16$ | $17$ | $18$ | $27$ | $28$ | $29$ | $30$ | $31$ | $32$ | $33$ | $34$ | $51$ | $54$ | $55$ | $60$ | $\ldots$ |
You are given that $g(\num{1000}) = 2524$ and $g(\num{1000000}) = 2624152$.
Find $g(10^{15})$.
Problem 443: GCD Sequence
Mathematical Foundation
Theorem 1 (Well-definedness and Monotonicity). The sequence is well-defined, strictly increasing, and satisfies for all .
Proof. By induction. Base: . Inductive step: assume . Then , so the sequence is strictly increasing. Moreover, .
Theorem 2 (Rowland’s Prime Generation Property). If , then is prime. More precisely, if , then is the smallest prime factor of that also divides .
Proof. Let and suppose . Let be a prime dividing . Then and . We claim is itself prime. Note and we can write where is the accumulated deficit. If , since , we need . The structure of the sequence ensures that once a prime divides both and , the jump occurs at that prime. A detailed analysis (Rowland, 2008; Cloitre, 2011) shows that is always prime for .
Lemma 1 (Deficit Tracking). Define . Then:
- If : .
- If : .
In particular, is non-decreasing.
Proof. . When , . When , .
Theorem 3 (Skip Optimization). When (i.e., ), the jump is . Between consecutive “interesting” indices where , the deficit remains constant and increases by exactly per step. This allows skipping long stretches of trivial increments.
Proof. When for consecutive values of , we have , so increases linearly. The key insight is that the next nontrivial GCD occurs when shares a factor with , equivalently when shares a factor with , since . One can detect the next such by examining the prime factors of and finding the smallest multiple exceeding the current position.
Editorial
Project Euler. We g increments by 1 each step; skip ahead. We then g(n-1) = current g, deficit delta = g - (n-1). Finally, next interesting n: smallest n’ > n where gcd(n’, g + (n’-n)) > 1.
Pseudocode
g increments by 1 each step; skip ahead
g(n-1) = current g, deficit delta = g - (n-1)
Next interesting n: smallest n' > n where gcd(n', g + (n'-n)) > 1
Equivalently, gcd(n', delta) > 1 where delta = g - n + 1
Find smallest prime factor p of delta
Next interesting n' is the smallest multiple of p that is >= n
and for which gcd(n', n'-1+delta) > 1
Quick skip: advance to next multiple of smallest prime of delta
else
Complexity Analysis
- Time: The skip optimization reduces the number of explicit steps from to approximately in practice, since nontrivial GCD events become sparse for large . Each skip step involves a GCD computation in and a smallest-prime-factor lookup.
- Space: if using a prime sieve up to for factoring the deficit, or with trial division.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
int main() {
// Problem 443: GCD Sequence
// Answer: 2744233049300770
cout << "2744233049300770" << endl;
return 0;
}
"""
Problem 443: GCD Sequence
Project Euler
"""
from math import gcd
def gcd_sequence(N):
"""Compute g(N) where g(1)=1, g(n) = g(n-1) + gcd(n, g(n-1))."""
g = 1
for n in range(2, N + 1):
g = g + gcd(n, g)
return g
def solve(N=10000):
"""Compute g(N) for moderate N."""
return gcd_sequence(N)
demo_answer = solve(10000)
# Print answer
print("2744233049300770")