All Euler problems
Project Euler

GCD of Divisors

Define f(n) = sum_(d | n) gcd(d, n/d) and F(k) = sum_(n=1)^k f(n). Given F(10) = 32 and F(1000) = 12776, find F(10^15).

Source sync Apr 19, 2026
Problem #0530
Level Level 22
Solved By 545
Languages C++, Python
Answer 207366437157977206
Length 281 words
number_theorylinear_algebramodular_arithmetic

Problem Statement

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

Every divisor \(d\) of a number \(n\) has a complementary divisor \(n/d\).

Let \(f(n)\) be the sum of the greatest common divisor of \(d\) and \(n/d\) over all positive divisors \(d\) of \(n\),

that is \(f(n)=\displaystyle \sum _{d\mid n}\gcd (d,\frac n d)\).

You are given that \(F(10)=32\) and \(F(1000)=12776\).

Find \(F(10^{15})\).

Problem 530: GCD of Divisors

Mathematical Foundation

Theorem (Multiplicativity of ff). The function ff is multiplicative: if gcd(a,b)=1\gcd(a, b) = 1, then f(ab)=f(a)f(b)f(ab) = f(a) \cdot f(b).

Proof. Since gcd(a,b)=1\gcd(a, b) = 1, the divisors of abab are exactly {d1d2:d1a,d2b}\{d_1 d_2 : d_1 \mid a, d_2 \mid b\}. For d=d1d2d = d_1 d_2 with d1ad_1 \mid a and d2bd_2 \mid b:

gcd(d,ab/d)=gcd(d1d2,(a/d1)(b/d2))=gcd(d1,a/d1)gcd(d2,b/d2)\gcd(d, ab/d) = \gcd(d_1 d_2, (a/d_1)(b/d_2)) = \gcd(d_1, a/d_1) \cdot \gcd(d_2, b/d_2)

where the last equality uses gcd(a,b)=1\gcd(a, b) = 1 (which implies gcd(d1,b/d2)=1\gcd(d_1, b/d_2) = 1 and gcd(d2,a/d1)=1\gcd(d_2, a/d_1) = 1). Therefore:

f(ab)=d1ad2bgcd(d1,a/d1)gcd(d2,b/d2)=f(a)f(b).f(ab) = \sum_{d_1 \mid a} \sum_{d_2 \mid b} \gcd(d_1, a/d_1) \cdot \gcd(d_2, b/d_2) = f(a) \cdot f(b).

\square

Lemma (Values at Prime Powers). For a prime pp and k1k \ge 1:

f(pk)=j=0kpmin(j,kj).f(p^k) = \sum_{j=0}^{k} p^{\min(j, k-j)}.

Explicitly:

f(pk)={2pk/21p1+pk/2if k is even,2p(k+1)/21p1if k is odd.f(p^k) = \begin{cases} 2\,\dfrac{p^{k/2} - 1}{p - 1} + p^{k/2} & \text{if } k \text{ is even,} \\[6pt] 2\,\dfrac{p^{(k+1)/2} - 1}{p - 1} & \text{if } k \text{ is odd.} \end{cases}

Proof. The divisors of pkp^k are pjp^j for 0jk0 \le j \le k. For divisor d=pjd = p^j, n/d=pkjn/d = p^{k-j}, so gcd(d,n/d)=pmin(j,kj)\gcd(d, n/d) = p^{\min(j, k-j)}.

For even kk: the sum is j=0kpmin(j,kj)=2j=0k/21pj+pk/2=2pk/21p1+pk/2\sum_{j=0}^{k} p^{\min(j,k-j)} = 2\sum_{j=0}^{k/2-1} p^j + p^{k/2} = 2 \cdot \frac{p^{k/2}-1}{p-1} + p^{k/2}.

For odd kk: the sum is 2j=0(k1)/2pj=2p(k+1)/21p12\sum_{j=0}^{(k-1)/2} p^j = 2 \cdot \frac{p^{(k+1)/2}-1}{p-1}. \square

Theorem (Summatory Formula via Substitution). For each divisor dd of nn, let g=gcd(d,n/d)g = \gcd(d, n/d). Write d=gad = ga and n/d=gbn/d = gb with gcd(a,b)=1\gcd(a, b) = 1, so n=g2abn = g^2 ab. Then:

F(k)=n=1kf(n)=g=1kg#{(a,b):gcd(a,b)=1,  g2abk}.F(k) = \sum_{n=1}^{k} f(n) = \sum_{g=1}^{\lfloor\sqrt{k}\rfloor} g \cdot \#\{(a, b) : \gcd(a, b) = 1,\; g^2 ab \le k\}.

Proof. Each triple (g,a,b)(g, a, b) with g1g \ge 1, a1a \ge 1, b1b \ge 1, gcd(a,b)=1\gcd(a, b) = 1, and g2abkg^2 ab \le k corresponds to exactly one pair (n,d)(n, d) with dnd \mid n, nkn \le k, and gcd(d,n/d)=g\gcd(d, n/d) = g: set n=g2abn = g^2 ab and d=gad = ga. The contribution to F(k)F(k) is gg. Conversely, given (n,d)(n, d) with dnd \mid n and g=gcd(d,n/d)g = \gcd(d, n/d), setting a=d/ga = d/g and b=(n/d)/gb = (n/d)/g recovers the triple with gcd(a,b)=1\gcd(a, b) = 1 and n=g2abn = g^2 ab. This is a bijection. \square

Lemma (Mobius Inversion for Coprime Pairs). The count of coprime pairs (a,b)(a, b) with abMab \le M is

e=1Mμ(e)D ⁣(Me2)\sum_{e=1}^{\lfloor\sqrt{M}\rfloor} \mu(e) \cdot D\!\left(\left\lfloor \frac{M}{e^2}\right\rfloor\right)

where D(x)=j=1xx/jD(x) = \sum_{j=1}^{x} \lfloor x/j \rfloor is the Dirichlet divisor sum.

Proof. Apply Mobius inversion to the coprimality constraint: #{(a,b):gcd(a,b)=1,abM}=e1μ(e)#{(a,b):abM/e2}\#\{(a,b) : \gcd(a,b)=1, ab \le M\} = \sum_{e \ge 1} \mu(e) \cdot \#\{(a',b') : a'b' \le M/e^2\}, where we substituted a=eaa = ea', b=ebb = eb'. The inner count #{(a,b):abM/e2}=D(M/e2)\#\{(a',b') : a'b' \le M/e^2\} = D(\lfloor M/e^2 \rfloor) by definition. The sum is finite since μ(e)=0\mu(e) = 0 for e2>Me^2 > M. \square

Lemma (Hyperbola Method for D(x)D(x)). The Dirichlet divisor sum satisfies

D(x)=2j=1xxjx2D(x) = 2\sum_{j=1}^{\lfloor\sqrt{x}\rfloor} \left\lfloor\frac{x}{j}\right\rfloor - \lfloor\sqrt{x}\rfloor^2

and is computable in O(x)O(\sqrt{x}) time.

Proof. By the Dirichlet hyperbola method: D(x)=abx1=a=1xx/a+b=1xx/bx2D(x) = \sum_{ab \le x} 1 = \sum_{a=1}^{\lfloor\sqrt{x}\rfloor} \lfloor x/a \rfloor + \sum_{b=1}^{\lfloor\sqrt{x}\rfloor} \lfloor x/b \rfloor - \lfloor\sqrt{x}\rfloor^2, which equals 2j=1xx/jx22\sum_{j=1}^{\lfloor\sqrt{x}\rfloor} \lfloor x/j \rfloor - \lfloor\sqrt{x}\rfloor^2. The sum has O(x)O(\sqrt{x}) terms. \square

Theorem (Final Formula). Combining the above:

F(k)=g=1kge1g2e2kμ(e)D ⁣(kg2e2).F(k) = \sum_{g=1}^{\lfloor\sqrt{k}\rfloor} g \sum_{\substack{e \ge 1 \\ g^2 e^2 \le k}} \mu(e) \cdot D\!\left(\left\lfloor\frac{k}{g^2 e^2}\right\rfloor\right).

Proof. Substitute M=k/g2M = \lfloor k/g^2 \rfloor into the Mobius inversion lemma, then sum over gg. \square

Editorial

f(n) = sum_{d|n} gcd(d, n/d) F(k) = sum_{n=1}^{k} f(n) Find F(10^15). F(k) = sum_{g=1}^{sqrt(k)} g * sum_{e: mu(e)!=0} mu(e) * D(k/(g^2*e^2)) where D(x) = Dirichlet divisor sum. We sieve Mobius function up to sqrt(k). We then compute F(k). Finally, hyperbola method for D(x).

Pseudocode

Sieve Mobius function up to sqrt(k)
Compute F(k)
Hyperbola method for D(x)
Standard sieve computing mu(n) for n = 1..limit

Complexity Analysis

  • Time: The outer sum over gg has O(k)O(\sqrt{k}) terms. For each gg, the inner sum over ee has O(k/g)O(\sqrt{k}/g) terms. For each (g,e)(g, e) pair, D(x)D(x) is computed in O(x)O(\sqrt{x}) time where x=k/(g2e2)x = \lfloor k/(g^2 e^2)\rfloor. The total cost is
g=1ke=1k/gk/(g2e2)=g=1ke=1k/gk1/2ge=O(k2/3).\sum_{g=1}^{\sqrt{k}} \sum_{e=1}^{\sqrt{k}/g} \sqrt{k/(g^2 e^2)} = \sum_{g=1}^{\sqrt{k}} \sum_{e=1}^{\sqrt{k}/g} \frac{k^{1/2}}{ge} = O(k^{2/3}).
  • Space: O(k)O(\sqrt{k}) for the Mobius function sieve.

Answer

207366437157977206\boxed{207366437157977206}

Code

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

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

/*
 * Project Euler Problem 530: GCD of Divisors
 *
 * f(n) = sum_{d|n} gcd(d, n/d)
 * F(k) = sum_{n=1}^{k} f(n)
 * Find F(10^15).
 *
 * F(k) = sum_{g=1}^{sqrt(k)} g * sum_{e: mu(e)!=0, g^2*e^2<=k} mu(e) * D(k/(g^2*e^2))
 * where D(x) = sum_{j=1}^{x} floor(x/j) (Dirichlet divisor sum)
 */

typedef long long ll;
typedef unsigned long long ull;

// Dirichlet divisor sum: D(x) = sum_{j=1}^{x} floor(x/j)
// Using hyperbola method: D(x) = 2*sum_{j=1}^{sqrt(x)} floor(x/j) - floor(sqrt(x))^2
ll dirichlet_divisor_sum(ll x) {
    if (x <= 0) return 0;
    ll sq = (ll)sqrt((double)x);
    while (sq * sq > x) sq--;
    while ((sq + 1) * (sq + 1) <= x) sq++;

    ll result = 0;
    for (ll j = 1; j <= sq; j++) {
        result += x / j;
    }
    result = 2 * result - sq * sq;
    return result;
}

int main() {
    // Verify small cases
    // F(10) = 32
    // Brute force for small values
    auto f_brute = [](int n) -> int {
        int sum = 0;
        for (int d = 1; d <= n; d++) {
            if (n % d == 0) {
                sum += __gcd(d, n / d);
            }
        }
        return sum;
    };

    ll F10 = 0;
    for (int n = 1; n <= 10; n++) F10 += f_brute(n);
    cout << "F(10) = " << F10 << endl;  // 32

    ll F1000 = 0;
    for (int n = 1; n <= 1000; n++) F1000 += f_brute(n);
    cout << "F(1000) = " << F1000 << endl;  // 12776

    // For F(10^15), use the formula with Mobius function
    ll N = 1000000000000000LL;  // 10^15
    ll sqN = (ll)sqrt((double)N);

    // Sieve Mobius function up to sqN
    int SIEVE_LIMIT = sqN + 10;
    vector<int> mu(SIEVE_LIMIT + 1, 1);
    vector<bool> is_prime(SIEVE_LIMIT + 1, true);
    vector<int> primes;

    // Compute Mobius function
    vector<int> min_factor(SIEVE_LIMIT + 1, 0);
    for (int i = 2; i <= SIEVE_LIMIT; i++) {
        if (is_prime[i]) {
            primes.push_back(i);
            min_factor[i] = i;
            for (ll j = (ll)i * i; j <= SIEVE_LIMIT; j += i) {
                is_prime[j] = false;
                if (min_factor[j] == 0) min_factor[j] = i;
            }
        }
    }

    // Compute mu properly
    mu[1] = 1;
    for (int i = 2; i <= SIEVE_LIMIT; i++) {
        if (is_prime[i]) {
            mu[i] = -1;
        } else {
            int p = min_factor[i];
            if ((i / p) % p == 0) {
                mu[i] = 0;
            } else {
                mu[i] = -mu[i / p];
            }
        }
    }

    // F(N) = sum_{g=1}^{sqrt(N)} g * sum_{e: mu(e)!=0, g^2*e^2<=N} mu(e) * D(N/(g^2*e^2))
    ll answer = 0;
    for (ll g = 1; g * g <= N; g++) {
        ll limit_e2 = N / (g * g);
        ll limit_e = (ll)sqrt((double)limit_e2);
        while (limit_e * limit_e > limit_e2) limit_e--;
        while ((limit_e + 1) * (limit_e + 1) <= limit_e2) limit_e++;

        for (ll e = 1; e <= limit_e; e++) {
            if (mu[e] == 0) continue;
            ll M = N / (g * g * e * e);
            ll D = dirichlet_divisor_sum(M);
            answer += g * mu[e] * D;
        }
    }

    cout << "F(10^15) = " << answer << endl;  // 207366437157977206

    return 0;
}