All Euler problems
Project Euler

The Inverse Summation of Coprime Couples

For an integer M >= 2, define R(M) = sum_(1 <= p < q <= M, p + q >= M, gcd(p,q) = 1) (1)/(pq). Let S(N) = sum_(M=2)^N R(M). Given that S(2) = 1/2, S(10) approx 6.9147, and S(100) approx 58.2962, fi...

Source sync Apr 19, 2026
Problem #0441
Level Level 24
Solved By 428
Languages C++, Python
Answer 5000088.8395
Length 353 words
number_theorygeometrydynamic_programming

Problem Statement

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

For an integer \(M\), we define \(R(M)\) as the sum of \(1/(p \cdot q)\) for all the integer pairs \(p\) and \(q\) which satisfy all of these conditions:

  • \(1 \leq p \lt q \leq M\)

  • \(p + q \geq M\)

  • \(p\) and \(q\) are coprime.

We also define \(S(N)\) as the sum of \(R(i)\) for \(2 \leq i \leq N\).

We can verify that \(S(2) = R(2) = 1/2\), \(S(10) \approx 6.9147\) and \(S(100) \approx 58.2962\).

Find \(S(10^7)\). Give your answer rounded to four decimal places.

Problem 441: The Inverse Summation of Coprime Couples

Solution

Theorem 1 (Contribution Counting)

Statement. For a coprime pair (p,q)(p,q) with 1p<q1 \leq p < q, define the multiplicity

c(p,q;N):={MZ:2MN,  qM,  p+qM}.c(p,q; N) := \bigl|\{M \in \mathbb{Z} : 2 \leq M \leq N,\; q \leq M,\; p + q \geq M\}\bigr|.

Then

S(N)=1p<qgcd(p,q)=11pqc(p,q;N),c(p,q;N)=max(0,min(N,p+q)q+1).S(N) = \sum_{\substack{1 \leq p < q \\ \gcd(p,q) = 1}} \frac{1}{pq}\, c(p,q; N), \qquad c(p,q; N) = \max\bigl(0,\, \min(N, p+q) - q + 1\bigr).

Proof. The pair (p,q)(p,q) with p<qp < q contributes the term 1pq\frac{1}{pq} to R(M)R(M) precisely when the three conditions 1p<qM1 \leq p < q \leq M and p+qMp + q \geq M and gcd(p,q)=1\gcd(p,q) = 1 all hold. The first two conditions reduce to qMp+qq \leq M \leq p + q. Since q2q \geq 2 (as p1p \geq 1 and p<qp < q), every such MM satisfies M2M \geq 2 automatically. Intersecting the interval [q,p+q][q, p+q] with [2,N][2, N] yields M[q,min(N,p+q)]M \in [\,q,\, \min(N, p+q)\,]. The number of integers in this interval is max(0,min(N,p+q)q+1)\max(0, \min(N, p+q) - q + 1). Exchanging the order of summation — summing first over pairs (p,q)(p,q), then over MM — gives the stated formula. \square

Lemma 1 (Full—Partial Decomposition)

Statement. For N2N \geq 2, every coprime pair (p,q)(p,q) with p<qNp < q \leq N falls into exactly one of two cases:

  1. Full pairs (p+qNp + q \leq N): The multiplicity is c(p,q;N)=p+1c(p,q;N) = p + 1.
  2. Partial pairs (p+q>Np + q > N): The multiplicity is c(p,q;N)=Nq+1c(p,q;N) = N - q + 1.

Consequently,

S(N)=p<qNp+qNgcd(p,q)=1p+1pq  +  p<qNp+q>Ngcd(p,q)=1Nq+1pq.S(N) = \sum_{\substack{p < q \leq N \\ p + q \leq N \\ \gcd(p,q)=1}} \frac{p+1}{pq} \;+\; \sum_{\substack{p < q \leq N \\ p + q > N \\ \gcd(p,q)=1}} \frac{N - q + 1}{pq}.

Proof. When p+qNp + q \leq N, we have min(N,p+q)=p+q\min(N, p+q) = p+q, so c=p+qq+1=p+1c = p + q - q + 1 = p + 1. When p+q>Np + q > N but qNq \leq N, we have min(N,p+q)=N\min(N, p+q) = N, so c=Nq+1c = N - q + 1. Pairs with q>Nq > N do not contribute. \square

Theorem 2 (Mobius Inversion)

Statement. Let f:Z>02Rf : \mathbb{Z}_{>0}^2 \to \mathbb{R} be an arithmetic function of two variables. Then

p,q1gcd(p,q)=1f(p,q)=d=1μ(d)a,b1f(da,db),\sum_{\substack{p,q \geq 1 \\ \gcd(p,q)=1}} f(p,q) = \sum_{d=1}^{\infty} \mu(d) \sum_{a,b \geq 1} f(da, db),

where μ\mu is the Mobius function, provided the right-hand side converges absolutely.

Proof. The classical identity [gcd(p,q)=1]=dgcd(p,q)μ(d)[\gcd(p,q) = 1] = \sum_{d \mid \gcd(p,q)} \mu(d) (a consequence of Mobius inversion on the poset of divisors of gcd(p,q)\gcd(p,q)) allows us to write

p,q1gcd(p,q)=1f(p,q)=p,q1f(p,q)dgcd(p,q)μ(d).\sum_{\substack{p,q \geq 1 \\ \gcd(p,q)=1}} f(p,q) = \sum_{p,q \geq 1} f(p,q) \sum_{d \mid \gcd(p,q)} \mu(d).

Interchanging summation (justified by absolute convergence) and substituting p=dap = da, q=dbq = db yields the result. \square

Lemma 2 (Harmonic Reduction)

Statement. After applying Theorem 2 with the substitution p=dap = da', q=dqq = dq', the inner sums factor through the harmonic numbers Hk=i=1k1iH_k = \sum_{i=1}^{k} \frac{1}{i}. Specifically, for fixed dd with μ(d)0\mu(d) \neq 0 and fixed qq', define Q=N/dQ = \lfloor N/d \rfloor and β=Qq\beta = Q - q'. Then:

  • Full-pair contribution (summing over p=1,,min(q1,β)p' = 1, \ldots, \min(q'-1, \beta)):
p=1Pfdp+1d2pq=Pfdq+HPfd2q,Pf=min(q1,β).\sum_{p'=1}^{P_f} \frac{dp' + 1}{d^2 p' q'} = \frac{P_f}{d\, q'} + \frac{H_{P_f}}{d^2\, q'}, \qquad P_f = \min(q'-1, \beta).
  • Partial-pair contribution (summing over p=max(1,β+1),,q1p' = \max(1, \beta+1), \ldots, q'-1):
p=Psq1Ndq+1d2pq=(Ndq+1)(Hq1HPs1)d2q.\sum_{p'=P_s}^{q'-1} \frac{N - dq' + 1}{d^2 p' q'} = \frac{(N - dq' + 1)(H_{q'-1} - H_{P_s - 1})}{d^2\, q'}.

Proof. Under the substitution p=dpp = dp', q=dqq = dq', the term 1pq=1d2pq\frac{1}{pq} = \frac{1}{d^2 p' q'}. For full pairs, dp+dqNdp' + dq' \leq N iff pQq=βp' \leq Q - q' = \beta, and the multiplicity is dp+1dp' + 1. The sum p=1Pfdp+1d2pq=1dqp=1Pf1+1d2qp=1Pf1p=Pfdq+HPfd2q\sum_{p'=1}^{P_f} \frac{dp' + 1}{d^2 p' q'} = \frac{1}{dq'} \sum_{p'=1}^{P_f} 1 + \frac{1}{d^2 q'} \sum_{p'=1}^{P_f} \frac{1}{p'} = \frac{P_f}{dq'} + \frac{H_{P_f}}{d^2 q'}. The partial-pair formula follows similarly, noting that Ndq+1N - dq' + 1 is constant with respect to pp'. \square

Editorial

Compute S(N) = sum_{M=2}^{N} R(M) where R(M) = sum_{1<=p<q<=M, p+q>=M, gcd(p,q)=1} 1/(pq). Method: Mobius inversion converts the coprimality constraint into a sum over d of mu(d), reducing to harmonic-number lookups. We sieve.** Compute μ(d)\mu(d) for d=1,,Nd = 1, \ldots, N via a linear sieve. We then harmonics.** Precompute Hk=Hk1+1/kH_k = H_{k-1} + 1/k for k=0,1,,Nk = 0, 1, \ldots, N in floating point. Finally, main loop.** For each dd with μ(d)0\mu(d) \neq 0, set Q=N/dQ = \lfloor N/d \rfloor. For each q=2,,Qq' = 2, \ldots, Q.

Pseudocode

Sieve.** Compute $\mu(d)$ for $d = 1, \ldots, N$ via a linear sieve
Harmonics.** Precompute $H_k = H_{k-1} + 1/k$ for $k = 0, 1, \ldots, N$ in floating point
Main loop.** For each $d$ with $\mu(d) \neq 0$, set $Q = \lfloor N/d \rfloor$. For each $q' = 2, \ldots, Q$:
Compute $P_f = \min(q'-1,\, Q - q')$. If $P_f \geq 1$, accumulate the full-pair contribution
Set $P_s = \max(1,\, Q - q' + 1)$. If $P_s \leq q' - 1$, accumulate the partial-pair contribution via harmonic differences
Aggregate.** Multiply each $d$-contribution by $\mu(d)$ and sum

Complexity Analysis

  • Time: O(NlogN)O(N \log N). The outer loop iterates over squarefree dNd \leq N. For each dd, the inner loop runs N/d1\lfloor N/d \rfloor - 1 iterations. The total work is d=1NN/d=O(NlnN)\sum_{d=1}^{N} N/d = O(N \ln N).
  • Space: O(N)O(N) for the Mobius function and harmonic number arrays.

Answer

5000088.8395\boxed{5000088.8395}

Code

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

C++ project_euler/problem_441/solution.cpp
/*
 * Project Euler 441: The Inverse Summation of Coprime Couples
 *
 * S(N) = sum_{M=2}^{N} R(M), where
 *   R(M) = sum_{1<=p<q<=M, p+q>=M, gcd(p,q)=1} 1/(pq).
 *
 * Via Mobius inversion and harmonic-number precomputation, we evaluate
 * S(10^7) in O(N log N) time.
 */
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;

static const int MAXN = 10000001;

int mu[MAXN];
double H[MAXN];

void sieve_mobius() {
    vector<int> primes;
    vector<bool> is_comp(MAXN, false);
    mu[1] = 1;
    for (int i = 2; i < MAXN; i++) {
        if (!is_comp[i]) {
            primes.push_back(i);
            mu[i] = -1;
        }
        for (int j = 0; j < (int)primes.size() && (long long)i * primes[j] < MAXN; j++) {
            is_comp[i * primes[j]] = true;
            if (i % primes[j] == 0) {
                mu[i * primes[j]] = 0;
                break;
            }
            mu[i * primes[j]] = -mu[i];
        }
    }
}

void precompute_harmonics() {
    H[0] = 0.0;
    for (int i = 1; i < MAXN; i++)
        H[i] = H[i - 1] + 1.0 / i;
}

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

    double ans = 0.0;

    for (int d = 1; d < MAXN; d++) {
        if (mu[d] == 0) continue;
        int Q = N / d;
        if (Q < 2) break;

        double contrib = 0.0;
        for (int q = 2; q <= Q; q++) {
            int pmax = q - 1;
            int boundary = Q - q;

            // Full pairs: p' in [1, min(pmax, boundary)]
            if (boundary >= 1) {
                int pf = min(pmax, boundary);
                contrib += (double)pf / ((double)d * q)
                         + H[pf] / ((double)d * d * q);
            }

            // Partial pairs: p' in [max(1, boundary+1), pmax]
            int ps = max(1, boundary + 1);
            if (ps <= pmax) {
                double factor = N - (double)d * q + 1.0;
                double harm = H[pmax] - H[ps - 1];
                contrib += factor * harm / ((double)d * d * q);
            }
        }

        ans += mu[d] * contrib;
    }

    printf("%.4f\n", ans);
    return 0;
}