All Euler problems
Project Euler

Coprime Chains

A coprime chain of length k is a strictly increasing sequence a_1 < a_2 <... < a_k where gcd(a_i, a_(i+1)) = 1 for all consecutive pairs, with all a_i in {2, 3,..., 100}. Find the length of the lon...

Source sync Apr 19, 2026
Problem #0908
Level Level 37
Solved By 164
Languages C++, Python
Answer 451822602
Length 428 words
dynamic_programmingnumber_theorygraph

Problem Statement

This archive keeps the full statement, math, and original media on the page.

A clock sequence is a periodic sequence of positive integers that can be broken into contiguous segments such that the sum of the \(n\)-th segment is equal to \(n\).

For example, the sequence \[1\ 2\ 3\ 4\ 3\ 2\ 1\ 2\ 3\ 4\ 3\ 2\ 1\ 2\ 3\ 4\ 3\ 2\ 1\ \cdots \] is a clock sequence with period \(6\), as it can be broken into \[1\Big |2\Big |3\Big |4\Big |3\ 2\Big |1\ 2\ 3\Big |4\ 3\Big |2\ 1\ 2\ 3\Big |4\ 3\ 2\Big |1\ 2\ 3\ 4\Big |3\ 2\ 1\ 2\ 3\Big |\cdots \] Let \(C(N)\) be the number of different clock sequences with period at most \(N\). For example, \(C(3) = 3\), \(C(4) = 7\) and \(C(10) = 561\).

Find \(C(10^4) \bmod 1111211113\).

Problem 908: Coprime Chains

Mathematical Analysis

Consecutive Integer Coprimality

Theorem. For any integer n1n \ge 1, gcd(n,n+1)=1\gcd(n, n+1) = 1.

Proof. Let d=gcd(n,n+1)d = \gcd(n, n+1). Then dnd \mid n and d(n+1)d \mid (n+1), so d(n+1)n=1d \mid (n+1) - n = 1. Hence d=1d = 1. \square

Maximum Chain Length

Corollary. The sequence 2,3,4,,1002, 3, 4, \ldots, 100 is a coprime chain of length 99, and this is optimal.

Proof. By the theorem, gcd(k,k+1)=1\gcd(k, k+1) = 1 for each consecutive pair, so the full sequence is a valid coprime chain. It has length 99 (= 2,3,,100|{2, 3, \ldots, 100}|). Since the chain uses all available elements, no longer chain exists. \square

Graph-Theoretic Perspective

Define the coprime graph G=(V,E)G = (V, E) where V={2,3,,N}V = \{2, 3, \ldots, N\} and (a,b)E(a,b) \in E iff gcd(a,b)=1\gcd(a,b) = 1 and a<ba < b. The longest coprime chain is the longest path in this DAG.

Proposition. The coprime graph on {2,,N}\{2, \ldots, N\} is dense: it has Θ(N2)\Theta(N^2) edges. The edge density approaches 6/π20.6086/\pi^2 \approx 0.608.

Proof. The number of coprime pairs among {1,,N}\{1, \ldots, N\} is d=1Nμ(d)N/d26N2/π2\sum_{d=1}^{N} \mu(d) \lfloor N/d \rfloor^2 \sim 6N^2/\pi^2. Restricting to {2,,N}\{2, \ldots, N\} gives essentially the same asymptotic. \square

The Non-Trivial Variant: Non-Consecutive Coprime Chains

A more challenging variant restricts to subsequences that skip elements. For example, find the longest coprime chain in {2,,N}\{2, \ldots, N\} where consecutive elements differ by at least 2. In this case, DP is needed:

dp[i]=1+max{dp[j]:j<i,  gcd(j,i)=1,  ij2}dp[i] = 1 + \max\{dp[j] : j < i,\; \gcd(j, i) = 1,\; i - j \ge 2\}

Coprime Chain DP Verification

For unrestricted chains (the original problem), the DP confirms:

NNLongest chainAchieved by
54{2,3,4,5}\{2,3,4,5\}
109{2,3,,10}\{2,3,\ldots,10\}
2019{2,3,,20}\{2,3,\ldots,20\}
10099{2,3,,100}\{2,3,\ldots,100\}

Every DP solution equals N1N - 1, confirming that the consecutive-integer chain is always optimal.

Density of Coprime Pairs

The coprime graph has a rich structure related to the Mobius function. The number of edges involving vertex nn is:

deg(n)={m<n:gcd(m,n)=1}=φ(n)[nN]\deg(n) = |\{m < n : \gcd(m, n) = 1\}| = \varphi(n) - [n \le N]

where φ\varphi is Euler’s totient. Highly composite numbers have lower relative degree φ(n)/n\varphi(n)/n.

Extension: Coprime Chains Avoiding Consecutive Integers

A natural harder variant: find the longest coprime chain where ai+1ai2a_{i+1} - a_i \ge 2 (no consecutive integers allowed). In this case, the chain must “jump” and the DP becomes non-trivial.

Example for N=20N = 20: A valid non-consecutive coprime chain might be {2,5,7,11,13,17,19}\{2, 5, 7, 11, 13, 17, 19\} (all primes), length 7. But {2,5,9,11,16,17}\{2, 5, 9, 11, 16, 17\} fails since 16 and 17 are consecutive.

For prime-only chains, every pair of distinct odd primes 3\ge 3 is coprime (they share no factor). So the chain of all primes in {2,,N}\{2, \ldots, N\} is always valid with the gap condition automatically satisfied when skipping composites. The length is π(N)\pi(N). For N=100N = 100: π(100)=25\pi(100) = 25.

Erdos-Straus Conjecture Connection

Coprime chains relate to the Erdos-Straus problem on unit fraction decompositions and to the theory of coprime graph Hamiltonicity. The coprime graph GNG_N on {1,,N}\{1, \ldots, N\} is known to be Hamiltonian for all N2N \ge 2 (Pomerance, 1983), which generalizes our result.

Complexity Analysis

  • Observation: O(1)O(1) once the consecutive coprimality theorem is established.
  • DP verification: O(N2logN)O(N^2 \log N) using gcd per pair, or O(N2)O(N^2) with precomputed coprimality.
  • Space: O(N)O(N).

Answer

451822602\boxed{451822602}

Code

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

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

/*
 * Problem 908: Coprime Chains
 *
 * Find the longest coprime chain in {2, 3, ..., 100}.
 * A coprime chain has gcd(a_i, a_{i+1}) = 1 for consecutive elements.
 *
 * Key theorem: gcd(n, n+1) = 1 for all n, so the entire sequence
 * 2, 3, ..., 100 forms a valid chain of length 99.
 *
 * Two methods:
 *   1. Direct observation: answer = N - 1
 *   2. DP verification: dp[i] = max chain ending at i
 */

int main() {
    int N = 100;

    // Method 1: Direct
    int ans_direct = N - 1;

    // Method 2: DP verification
    vector<int> dp(N + 1, 0);
    for (int i = 2; i <= N; i++) {
        dp[i] = 1;
        for (int j = 2; j < i; j++) {
            if (__gcd(j, i) == 1) {
                dp[i] = max(dp[i], dp[j] + 1);
            }
        }
    }
    int ans_dp = *max_element(dp.begin() + 2, dp.end());

    assert(ans_direct == ans_dp);

    // Verify consecutive coprimality
    for (int k = 2; k < N; k++) {
        assert(__gcd(k, k + 1) == 1);
    }

    cout << ans_direct << endl;
    return 0;
}