All Euler problems
Project Euler

Not Coprime

For a positive integer N, define C(N) as the number of pairs (a, b) with 1 <= a <= b <= N such that gcd(a, b) > 1 (i.e., a and b are not coprime). Compute C(10^7).

Source sync Apr 19, 2026
Problem #0838
Level Level 19
Solved By 706
Languages C++, Python
Answer 250591.442792
Length 317 words
number_theorymodular_arithmeticprobability

Problem Statement

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

Let \(f(N)\) be the smallest positive integer that is not coprime to any positive integer \(n \le N\) whose least significant digit is \(3\).

For example \(f(40)\) equals to \(897 = 3 \cdot 13 \cdot 23\) since it is not coprime to any of \(3,13,23,33\). By taking the natural logarithm (log to base \(e\)) we obtain \(\ln f(40) = \ln 897 \approx 6.799056\) when rounded to six digits after the decimal point.

You are also given \(\ln f(2800) \approx 715.019337\).

Find \(f(10^6)\). Enter its natural logarithm rounded to six digits after the decimal point.

Problem 838: Not Coprime

Mathematical Foundation

Theorem 1 (Mobius Coprime Count). The number of ordered pairs (a,b)(a,b) with 1a,bN1 \le a, b \le N and gcd(a,b)=1\gcd(a,b) = 1 is

Φ(N)=d=1Nμ(d)Nd2.\Phi(N) = \sum_{d=1}^{N} \mu(d) \left\lfloor \frac{N}{d} \right\rfloor^2.

Proof. We use the fundamental identity [gcd(a,b)=1]=dgcd(a,b)μ(d)[\gcd(a,b)=1] = \sum_{d \mid \gcd(a,b)} \mu(d), which follows from the Mobius inversion formula applied to the constant function 1\mathbf{1} and its Dirichlet inverse μ\mu. Summing over all ordered pairs:

Φ(N)=a=1Nb=1Ndgcd(a,b)μ(d)=d=1Nμ(d)1aNda1bNdb1=d=1Nμ(d)Nd2.\Phi(N) = \sum_{a=1}^{N}\sum_{b=1}^{N} \sum_{d \mid \gcd(a,b)} \mu(d) = \sum_{d=1}^{N} \mu(d) \sum_{\substack{1 \le a \le N \\ d \mid a}} \sum_{\substack{1 \le b \le N \\ d \mid b}} 1 = \sum_{d=1}^{N} \mu(d) \left\lfloor \frac{N}{d} \right\rfloor^2.

The exchange of summation is justified by absolute convergence (all sums are finite). The inner sums count multiples of dd in {1,,N}\{1, \ldots, N\}, each equaling N/d\lfloor N/d \rfloor. \square

Lemma 1 (Ordered to Unordered). The number of unordered coprime pairs (a,b)(a, b) with 1abN1 \le a \le b \le N is

Ccop(N)=Φ(N)+12.C_{\mathrm{cop}}(N) = \frac{\Phi(N) + 1}{2}.

Proof. Among the Φ(N)\Phi(N) ordered coprime pairs, the only pair with a=ba = b and gcd(a,a)=1\gcd(a,a) = 1 is (1,1)(1,1). All other coprime pairs (a,b)(a,b) with aba \ne b appear twice (as (a,b)(a,b) and (b,a)(b,a)). Hence the number of unordered pairs is Φ(N)12+1=Φ(N)+12\frac{\Phi(N) - 1}{2} + 1 = \frac{\Phi(N) + 1}{2}. \square

Theorem 2 (Non-Coprime Count). The number of unordered non-coprime pairs is

C(N)=N(N+1)2Φ(N)+12.C(N) = \frac{N(N+1)}{2} - \frac{\Phi(N)+1}{2}.

Proof. The total number of unordered pairs (a,b)(a,b) with 1abN1 \le a \le b \le N is (N2)+N=N(N+1)2\binom{N}{2} + N = \frac{N(N+1)}{2} (including pairs with a=ba = b). Subtracting the coprime pairs: C(N)=N(N+1)2Ccop(N)C(N) = \frac{N(N+1)}{2} - C_{\mathrm{cop}}(N). \square

Theorem 3 (Hyperbola Method). The sum d=1Nμ(d)N/d2\sum_{d=1}^{N} \mu(d) \lfloor N/d \rfloor^2 can be evaluated in O(N)O(\sqrt{N}) arithmetic operations, given prefix sums of the Mobius function M(x)=k=1xμ(k)M(x) = \sum_{k=1}^{x} \mu(k).

Proof. The function dq(d)=N/dd \mapsto q(d) = \lfloor N/d \rfloor takes at most 2N2\lfloor\sqrt{N}\rfloor distinct values. For each distinct value qq, the set of dd with N/d=q\lfloor N/d \rfloor = q forms a contiguous interval [d1,d2][d_1, d_2] where d2=N/qd_2 = \lfloor N/q \rfloor and d1=N/(q+1)+1d_1 = \lfloor N/(q+1) \rfloor + 1. The contribution of this block is

q2d=d1d2μ(d)=q2(M(d2)M(d11)).q^2 \sum_{d=d_1}^{d_2} \mu(d) = q^2 \bigl(M(d_2) - M(d_1 - 1)\bigr).

Summing over all O(N)O(\sqrt{N}) blocks gives the result. \square

Lemma 2 (Asymptotic Density). As NN \to \infty, Φ(N)6N2π2\Phi(N) \sim \frac{6N^2}{\pi^2}, so the probability that two random integers are coprime is 6/π20.60796/\pi^2 \approx 0.6079.

Proof. Φ(N)/N2=d=1Nμ(d)/d2+O(1/N)d=1μ(d)/d2=p(11/p2)=1/ζ(2)=6/π2\Phi(N)/N^2 = \sum_{d=1}^{N} \mu(d)/d^2 + O(1/N) \to \sum_{d=1}^{\infty} \mu(d)/d^2 = \prod_p (1 - 1/p^2) = 1/\zeta(2) = 6/\pi^2, where the Euler product follows from the multiplicativity of μ\mu and the product formula for ζ(2)\zeta(2). \square

Editorial

Count pairs (a, b) with 1 <= a <= b <= N such that gcd(a, b) > 1. Key identity: Phi(N) = sum_{d=1}^{N} mu(d) * floor(N/d)^2 counts ordered coprime pairs, then C(N) = N(N+1)/2 - (Phi(N)+1)/2. We linear sieve for Mobius function. We then iterate over p in primes. Finally, prefix sums (Mertens function).

Pseudocode

Linear sieve for Mobius function
for p in primes
Prefix sums (Mertens function)
Block summation (hyperbola method)
Compute answer

Complexity Analysis

  • Time: O(N)O(N) for the linear sieve and prefix sums; O(N)O(\sqrt{N}) for the block summation. Total: O(N)O(N).
  • Space: O(N)O(N) for storing μ\mu and MM.

Answer

250591.442792\boxed{250591.442792}

Code

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

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

/*
 * Problem 838: Not Coprime
 *
 * Count pairs (a,b) with 1 <= a <= b <= N and gcd(a,b) > 1.
 *
 * Method: Mobius sieve + block summation.
 *   Phi(N) = sum_{d=1}^{N} mu(d) * floor(N/d)^2  (ordered coprime pairs)
 *   C(N) = N(N+1)/2 - (Phi(N)+1)/2
 *
 * Two methods implemented for cross-verification.
 */

const int MAXN = 10000001;

int mu[MAXN];
long long M[MAXN];  // Mertens function prefix sums
bool is_prime[MAXN];
vector<int> primes;

void sieve_mobius(int n) {
    fill(is_prime, is_prime + n + 1, true);
    mu[1] = 1;
    for (int i = 2; i <= n; i++) {
        if (is_prime[i]) {
            primes.push_back(i);
            mu[i] = -1;
        }
        for (int p : primes) {
            if ((long long)i * p > n) break;
            is_prime[i * p] = false;
            if (i % p == 0) {
                mu[i * p] = 0;
                break;
            }
            mu[i * p] = -mu[i];
        }
    }
    // Compute Mertens function
    M[0] = 0;
    for (int i = 1; i <= n; i++) {
        M[i] = M[i - 1] + mu[i];
    }
}

// Method 1: Block summation using Mertens prefix sums
long long solve_block(int N) {
    long long phi_N = 0;
    int d = 1;
    while (d <= N) {
        long long q = N / d;
        int d2 = N / q;
        phi_N += q * q * (M[d2] - M[d - 1]);
        d = d2 + 1;
    }
    long long total = (long long)N * (N + 1) / 2;
    long long coprime = (phi_N + 1) / 2;
    return total - coprime;
}

// Method 2: Direct summation (slower, for verification)
long long solve_direct(int N) {
    long long phi_N = 0;
    for (int d = 1; d <= N; d++) {
        long long q = N / d;
        phi_N += (long long)mu[d] * q * q;
    }
    long long total = (long long)N * (N + 1) / 2;
    long long coprime = (phi_N + 1) / 2;
    return total - coprime;
}

int main() {
    int N = 10000000;
    sieve_mobius(N);

    long long ans1 = solve_block(N);
    long long ans2 = solve_direct(N);

    assert(ans1 == ans2);
    cout << ans1 << endl;
    return 0;
}