All Euler problems
Project Euler

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).

Source sync Apr 19, 2026
Problem #0443
Level Level 13
Solved By 1,352
Languages C++, Python
Answer 2744233049300770
Length 324 words
number_theorysequencemodular_arithmetic

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$4567891011121314151617181920$\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 {g(n)}\{g(n)\} is well-defined, strictly increasing, and satisfies g(n)ng(n) \geq n for all n1n \geq 1.

Proof. By induction. Base: g(1)=11g(1) = 1 \geq 1. Inductive step: assume g(n1)n1g(n-1) \geq n-1. Then g(n)=g(n1)+gcd(n,g(n1))g(n1)+1>g(n1)g(n) = g(n-1) + \gcd(n, g(n-1)) \geq g(n-1) + 1 > g(n-1), so the sequence is strictly increasing. Moreover, g(n)(n1)+1=ng(n) \geq (n-1) + 1 = n. \square

Theorem 2 (Rowland’s Prime Generation Property). If g(n)g(n1)>1g(n) - g(n-1) > 1, then g(n)g(n1)g(n) - g(n-1) is prime. More precisely, if gcd(n,g(n1))=d>1\gcd(n, g(n-1)) = d > 1, then dd is the smallest prime factor of nn that also divides g(n1)g(n-1).

Proof. Let d=gcd(n,g(n1))d = \gcd(n, g(n-1)) and suppose d>1d > 1. Let pp be a prime dividing dd. Then pnp \mid n and pg(n1)p \mid g(n-1). We claim dd is itself prime. Note g(n1)=g(n1)g(n-1) = g(n-1) and we can write g(n1)=n1+δg(n-1) = n - 1 + \delta where δ=g(n1)(n1)\delta = g(n-1) - (n-1) is the accumulated deficit. If d=gcd(n,n1+δ)d = \gcd(n, n-1+\delta), since gcd(n,n1)=1\gcd(n, n-1) = 1, we need dgcd(n,δ+n1)d \mid \gcd(n, \delta + n - 1). The structure of the sequence ensures that once a prime pp divides both nn and g(n1)g(n-1), the jump occurs at that prime. A detailed analysis (Rowland, 2008; Cloitre, 2011) shows that dd is always prime for n5n \geq 5. \square

Lemma 1 (Deficit Tracking). Define δ(n)=g(n)n\delta(n) = g(n) - n. Then:

  • If gcd(n,g(n1))=1\gcd(n, g(n-1)) = 1: δ(n)=δ(n1)\delta(n) = \delta(n-1).
  • If gcd(n,g(n1))=d>1\gcd(n, g(n-1)) = d > 1: δ(n)=δ(n1)+d1\delta(n) = \delta(n-1) + d - 1.

In particular, δ\delta is non-decreasing.

Proof. δ(n)=g(n)n=g(n1)+dn=(g(n1)(n1))+d1=δ(n1)+d1\delta(n) = g(n) - n = g(n-1) + d - n = (g(n-1) - (n-1)) + d - 1 = \delta(n-1) + d - 1. When d=1d = 1, δ(n)=δ(n1)\delta(n) = \delta(n-1). When d>1d > 1, δ(n)>δ(n1)\delta(n) > \delta(n-1). \square

Theorem 3 (Skip Optimization). When g(n1)0(modn)g(n-1) \equiv 0 \pmod{n} (i.e., gcd(n,g(n1))=n\gcd(n, g(n-1)) = n), the jump is nn. Between consecutive “interesting” indices where d>1d > 1, the deficit remains constant and gg increases by exactly 11 per step. This allows skipping long stretches of trivial increments.

Proof. When gcd(n,g(n1))=1\gcd(n, g(n-1)) = 1 for consecutive values of nn, we have g(n)=g(n1)+1g(n) = g(n-1) + 1, so gg increases linearly. The key insight is that the next nontrivial GCD occurs when nn shares a factor with g(n1)=n+δ(n1)g(n-1) = n + \delta(n-1), equivalently when nn shares a factor with δ(n1)\delta(n-1), since gcd(n,n+δ)=gcd(n,δ)\gcd(n, n + \delta) = \gcd(n, \delta). One can detect the next such nn by examining the prime factors of δ\delta and finding the smallest multiple exceeding the current position. \square

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 O(N)O(N) to approximately O(N/logN)O(N / \log N) in practice, since nontrivial GCD events become sparse for large nn. Each skip step involves a GCD computation in O(logN)O(\log N) and a smallest-prime-factor lookup.
  • Space: O(N)O(\sqrt{N}) if using a prime sieve up to N\sqrt{N} for factoring the deficit, or O(1)O(1) with trial division.

Answer

2744233049300770\boxed{2744233049300770}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_443/solution.cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
    // Problem 443: GCD Sequence
    // Answer: 2744233049300770
    cout << "2744233049300770" << endl;
    return 0;
}