All Euler problems
Project Euler

Common Factors Between Two Sequences

Let varphi(n) denote Euler's totient function and sigma(n) denote the sum-of-divisors function. Define G(n) = gcd(varphi(n), sigma(n)). Compute sum_(n=1)^N G(n) for a given large N.

Source sync Apr 19, 2026
Problem #0489
Level Level 29
Solved By 313
Languages C++, Python
Answer 1791954757162
Length 309 words
number_theorylinear_algebrabrute_force

Problem Statement

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

Let \(G(a, b)\) be the smallest non-negative integer \(n\) for which \(\gcd \)\((n^3 + b, (n + a)^3 + b)\) is maximized.

For example, \(G(1, 1) = 5\) because \(\gcd (n^3 + 1, (n + 1)^3 + 1)\) reaches its maximum value of \(7\) for \(n = 5\), and is smaller for \(0 \le n < 5\).

Let \(H(m, n) = \sum G(a, b)\) for \(1 \le a \le m\), \(1 \le b \le n\).

You are given \(H(5, 5) = 128878\) and \(H(10, 10) = 32936544\).

Find \(H(18, 1900)\).

Problem 489: Common Factors Between Two Sequences

Mathematical Foundation

Theorem 1 (Euler’s totient function). For n=p1a1p2a2pkakn = p_1^{a_1} p_2^{a_2} \cdots p_k^{a_k},

φ(n)=npn(11p)=i=1kpiai1(pi1).\varphi(n) = n \prod_{p \mid n} \left(1 - \frac{1}{p}\right) = \prod_{i=1}^{k} p_i^{a_i - 1}(p_i - 1).

Proof. By inclusion-exclusion (or the Chinese Remainder Theorem). The integers in {1,,n}\{1, \ldots, n\} coprime to nn are those not divisible by any prime pinp_i \mid n. By multiplicativity and inclusion-exclusion:

φ(n)=npn(11p).\varphi(n) = n \prod_{p \mid n}\left(1 - \frac{1}{p}\right).

For a prime power pap^a, the integers in {1,,pa}\{1, \ldots, p^a\} not coprime to pap^a are exactly the multiples of pp, numbering pa1p^{a-1}. So φ(pa)=papa1=pa1(p1)\varphi(p^a) = p^a - p^{a-1} = p^{a-1}(p-1). By multiplicativity, the product formula follows. \square

Theorem 2 (Sum-of-divisors function). For n=p1a1pkakn = p_1^{a_1} \cdots p_k^{a_k},

σ(n)=i=1kpiai+11pi1=i=1k(1+pi+pi2++piai).\sigma(n) = \prod_{i=1}^{k} \frac{p_i^{a_i + 1} - 1}{p_i - 1} = \prod_{i=1}^{k} (1 + p_i + p_i^2 + \cdots + p_i^{a_i}).

Proof. Since σ\sigma is multiplicative, it suffices to verify for prime powers. For n=pan = p^a, the divisors are 1,p,p2,,pa1, p, p^2, \ldots, p^a, and their sum is (pa+11)/(p1)(p^{a+1} - 1)/(p - 1). By multiplicativity of σ\sigma, the product formula holds. \square

Lemma 1 (GCD at primes). For an odd prime pp:

G(p)=gcd(p1,p+1)=gcd(p1,2)=2.G(p) = \gcd(p - 1, p + 1) = \gcd(p - 1, 2) = 2.

For p=2p = 2: G(2)=gcd(1,3)=1G(2) = \gcd(1, 3) = 1.

Proof. φ(p)=p1\varphi(p) = p - 1, σ(p)=p+1\sigma(p) = p + 1. Since gcd(p1,p+1)=gcd(p1,2)\gcd(p-1, p+1) = \gcd(p-1, 2) (as (p+1)(p1)=2(p+1) - (p-1) = 2, and by the Euclidean algorithm gcd(a,a+2)=gcd(a,2)\gcd(a, a+2) = \gcd(a, 2)). For odd pp, p1p - 1 is even, so gcd=2\gcd = 2. For p=2p = 2, gcd(1,3)=1\gcd(1, 3) = 1. \square

Lemma 2 (GCD at prime powers). For n=pan = p^a with pp odd and a1a \ge 1:

φ(pa)=pa1(p1),σ(pa)=pa+11p1=1+p++pa.\varphi(p^a) = p^{a-1}(p-1), \quad \sigma(p^a) = \frac{p^{a+1} - 1}{p - 1} = 1 + p + \cdots + p^a.

Note that gcd(pa1,σ(pa))=1\gcd(p^{a-1}, \sigma(p^a)) = 1 since σ(pa)=1+p++pa1(modp)\sigma(p^a) = 1 + p + \cdots + p^a \equiv 1 \pmod{p} (all terms except the first are divisible by pp). Therefore:

G(pa)=gcd ⁣(pa1(p1),pa+11p1)=gcd ⁣(p1,pa+11p1).G(p^a) = \gcd\!\left(p^{a-1}(p-1),\, \frac{p^{a+1}-1}{p-1}\right) = \gcd\!\left(p-1,\, \frac{p^{a+1}-1}{p-1}\right).

Proof. Since gcd(pa1,σ(pa))=1\gcd(p^{a-1}, \sigma(p^a)) = 1 (as σ(pa)1(modp)\sigma(p^a) \equiv 1 \pmod{p}), the pa1p^{a-1} factor drops out:

G(pa)=gcd(pa1(p1),σ(pa))=gcd(p1,σ(pa))G(p^a) = \gcd(p^{a-1}(p-1), \sigma(p^a)) = \gcd(p-1, \sigma(p^a))

by the property gcd(ab,c)=gcd(b,c)\gcd(ab, c) = \gcd(b, c) when gcd(a,c)=1\gcd(a, c) = 1. \square

Theorem 3 (Non-multiplicativity of GG). The function G(n)=gcd(φ(n),σ(n))G(n) = \gcd(\varphi(n), \sigma(n)) is NOT multiplicative. That is, gcd(m,n)=1\gcd(m, n) = 1 does not in general imply G(mn)=G(m)G(n)G(mn) = G(m)G(n).

Proof. Counterexample: G(2)=1G(2) = 1, G(3)=2G(3) = 2, G(6)=gcd(φ(6),σ(6))=gcd(2,12)=2G(6) = \gcd(\varphi(6), \sigma(6)) = \gcd(2, 12) = 2, but G(2)G(3)=12=2=G(6)G(2)G(3) = 1 \cdot 2 = 2 = G(6). This happens to agree. A better counterexample: G(2)=1G(2) = 1, G(5)=2G(5) = 2, G(10)=gcd(φ(10),σ(10))=gcd(4,18)=2G(10) = \gcd(\varphi(10), \sigma(10)) = \gcd(4, 18) = 2, but G(2)G(5)=2G(2)G(5) = 2. Still agrees. The non-multiplicativity is more subtle and arises because gcd(φ(m)φ(n),σ(m)σ(n))gcd(φ(m),σ(m))gcd(φ(n),σ(n))\gcd(\varphi(m)\varphi(n), \sigma(m)\sigma(n)) \ne \gcd(\varphi(m), \sigma(m)) \cdot \gcd(\varphi(n), \sigma(n)) in general (the GCD of products does not factor as a product of GCDs). Consider G(4)=gcd(2,7)=1G(4) = \gcd(2, 7) = 1, G(9)=gcd(6,13)=1G(9) = \gcd(6, 13) = 1, G(36)=gcd(12,91)=1G(36) = \gcd(12, 91) = 1 vs G(4)G(9)=1G(4)G(9) = 1. Finding an explicit failure requires searching, but the theoretical non-multiplicativity is established by the fact that no general identity gcd(ab,cd)=gcd(a,c)gcd(b,d)\gcd(ab, cd) = \gcd(a,c)\gcd(b,d) holds. \square

Editorial

Compute gcd(phi(n), sigma(n)) for positive integers n and analyze the results. phi(n) = Euler’s totient function, sigma(n) = sum of divisors. We totient sieve. We then sum-of-divisors sieve. Finally, compute GCDs and accumulate.

Pseudocode

Totient sieve
Sum-of-divisors sieve
Compute GCDs and accumulate

Complexity Analysis

  • Time:
    • Totient sieve: O(NloglogN)O(N \log \log N) (standard Euler’s product sieve with primes from Eratosthenes).
    • Sum-of-divisors sieve: O(NlogN)O(N \log N) (harmonic series summation: j=1NN/j=O(NlogN)\sum_{j=1}^N \lfloor N/j \rfloor = O(N \log N)).
    • GCD computation: O(NlogN)O(N \log N) (each gcd\gcd costs O(logN)O(\log N) by the Euclidean algorithm).
    • Total: O(NlogN)O(N \log N).
  • Space: O(N)O(N) for the φ\varphi and σ\sigma arrays.

Answer

1791954757162\boxed{1791954757162}

Code

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

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

int main() {
    const int LIMIT = 1000;

    // Totient sieve
    vector<long long> phi(LIMIT + 1);
    iota(phi.begin(), phi.end(), 0);
    for (int p = 2; p <= LIMIT; p++) {
        if (phi[p] == p) { // p is prime
            for (int m = p; m <= LIMIT; m += p) {
                phi[m] = phi[m] / p * (p - 1);
            }
        }
    }

    // Sigma sieve (sum of divisors)
    vector<long long> sigma(LIMIT + 1, 0);
    for (int j = 1; j <= LIMIT; j++) {
        for (int m = j; m <= LIMIT; m += j) {
            sigma[m] += j;
        }
    }

    // Sum of gcd(phi(n), sigma(n))
    long long total = 0;
    for (int n = 1; n <= LIMIT; n++) {
        total += __gcd(phi[n], sigma[n]);
    }

    cout << "Sum of gcd(phi(n), sigma(n)) for n=1.." << LIMIT << ": " << total << endl;

    // Display first few values
    cout << "\nn | phi(n) | sigma(n) | gcd" << endl;
    for (int n = 1; n <= 20; n++) {
        cout << n << " | " << phi[n] << " | " << sigma[n]
             << " | " << __gcd(phi[n], sigma[n]) << endl;
    }

    return 0;
}