All Euler problems
Project Euler

abc-hits

The radical of n, rad(n), is the product of the distinct prime factors of n. A triple (a, b, c) is an abc-hit if: 1. gcd(a, b) = 1 2. a < b 3. a + b = c 4. rad(abc) < c Find sum c for all abc-hits...

Source sync Apr 19, 2026
Problem #0127
Level Level 05
Solved By 7,293
Languages C++, Python
Answer 18407904
Length 245 words
number_theorylinear_algebrasearch

Problem Statement

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

The radical of $n$, $\operatorname{rad}(n)$, is the product of distinct prime factors of $n$. For example, $504 = 2^3 \times 3^2 \times 7$, so $\operatorname{rad}(504) = 2 \times 3 \times 7 = 42$.

We shall define the triplet of positive integers $(a, b, c)$ to be an abc-hit if:

  1. $\gcd(a, b) = \gcd(a, c) = \gcd(b, c) = 1$

  2. $a < b$

  3. $a + b = c$

  4. $\operatorname{rad}(abc) < c$

For example, $(5, 27, 32)$ is an abc-hit, because:

  1. $\gcd(5, 27) = \gcd(5, 32) = \gcd(27, 32) = 1$

  2. $5 < 27$

  3. $5 + 27 = 32$

  4. $\operatorname{4320}{4320} = 30 < 32$

It turns out that abc-hits are quite rare and there are only thirty-one abc-hits for $c < 1000$, with $\sum c = 12523$.

Find $\displaystyle \sum c$ for $c < 120000$.

Problem 127: abc-hits

Mathematical Foundation

Theorem 1 (Pairwise coprimality). If gcd(a,b)=1\gcd(a, b) = 1 and a+b=ca + b = c, then aa, bb, cc are pairwise coprime.

Proof. Suppose a prime pp divides both aa and c=a+bc = a + b. Then p(ca)=bp \mid (c - a) = b, contradicting gcd(a,b)=1\gcd(a, b) = 1. By symmetry (swapping aa and bb), gcd(b,c)=1\gcd(b, c) = 1 as well. \square

Theorem 2 (Radical factorization for coprime triples). If aa, bb, cc are pairwise coprime, then rad(abc)=rad(a)rad(b)rad(c)\text{rad}(abc) = \text{rad}(a) \cdot \text{rad}(b) \cdot \text{rad}(c).

Proof. Since aa, bb, cc share no common prime factor, the set of primes dividing abcabc is the disjoint union of primes dividing aa, bb, and cc respectively. Therefore pabcp=pappbppcp\prod_{p \mid abc} p = \prod_{p \mid a} p \cdot \prod_{p \mid b} p \cdot \prod_{p \mid c} p. \square

Lemma 1 (Reformulated condition). The abc-hit condition rad(abc)<c\text{rad}(abc) < c is equivalent to rad(a)rad(b)<c/rad(c)\text{rad}(a) \cdot \text{rad}(b) < c / \text{rad}(c).

Proof. By Theorem 2, rad(abc)=rad(a)rad(b)rad(c)\text{rad}(abc) = \text{rad}(a) \cdot \text{rad}(b) \cdot \text{rad}(c). The inequality rad(a)rad(b)rad(c)<c\text{rad}(a) \cdot \text{rad}(b) \cdot \text{rad}(c) < c divides both sides by rad(c)>0\text{rad}(c) > 0. \square

Lemma 2 (Coprimality via radicals). For positive integers aa and bb, gcd(a,b)=1\gcd(a, b) = 1 if and only if gcd(rad(a),rad(b))=1\gcd(\text{rad}(a), \text{rad}(b)) = 1.

Proof. ()(\Rightarrow): If a prime pp divides both rad(a)\text{rad}(a) and rad(b)\text{rad}(b), then pap \mid a and pbp \mid b, contradicting gcd(a,b)=1\gcd(a,b) = 1. ()(\Leftarrow): If pgcd(a,b)p \mid \gcd(a,b), then prad(a)p \mid \text{rad}(a) and prad(b)p \mid \text{rad}(b), contradicting gcd(rad(a),rad(b))=1\gcd(\text{rad}(a), \text{rad}(b)) = 1. \square

Theorem 3 (Search strategy). For each cc, sorting candidates aa by rad(a)\text{rad}(a) in ascending order allows early termination: once rad(a)c/rad(c)\text{rad}(a) \geq c / \text{rad}(c), no further aa can satisfy the condition (since rad(b)1\text{rad}(b) \geq 1).

Proof. If rad(a)c/rad(c)\text{rad}(a) \geq c / \text{rad}(c), then rad(a)rad(b)rad(a)c/rad(c)\text{rad}(a) \cdot \text{rad}(b) \geq \text{rad}(a) \geq c / \text{rad}(c) for all bb. Thus the condition rad(a)rad(b)<c/rad(c)\text{rad}(a) \cdot \text{rad}(b) < c / \text{rad}(c) fails, and all subsequent aa (with equal or larger radical) also fail. \square

Editorial

An abc-hit: gcd(a,b)=1, a<b, a+b=c, rad(abc) < c. We sieve for radicals. We then sort indices by radical. Finally, find abc-hits.

Pseudocode

Sieve for radicals
Sort indices by radical
Find abc-hits

Complexity Analysis

  • Time: O(NloglogN)O(N \log \log N) for the sieve. O(NlogN)O(N \log N) for sorting. The main search loop: for each cc, the inner loop terminates early due to the radical threshold. Empirically, the total work across all cc is O(NlogN)O(N \log N).
  • Space: O(N)O(N) for the radical array and sorted index.

Answer

18407904\boxed{18407904}

Code

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

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

int main() {
    const int N = 120000;
    vector<int> rad(N, 1);

    // Sieve for radicals
    for (int p = 2; p < N; p++) {
        if (rad[p] == 1) { // p is prime
            for (int m = p; m < N; m += p) {
                rad[m] *= p;
            }
        }
    }

    // Sort indices 1..N-1 by radical (skip 0)
    vector<int> sorted_by_rad;
    sorted_by_rad.reserve(N - 1);
    for (int i = 1; i < N; i++) sorted_by_rad.push_back(i);
    sort(sorted_by_rad.begin(), sorted_by_rad.end(),
         [&](int x, int y) { return rad[x] < rad[y]; });

    long long total = 0;

    for (int c = 3; c < N; c++) {
        long long threshold = (long long)c / rad[c];
        if (threshold <= 1) continue;

        for (int i = 0; i < (int)sorted_by_rad.size(); i++) {
            int a = sorted_by_rad[i];
            if (rad[a] >= threshold) break;
            if (a >= c) continue;
            int b = c - a;
            if (a >= b) continue;
            if ((long long)rad[a] * rad[b] >= threshold) continue;
            if (__gcd(rad[a], rad[b]) != 1) continue;
            total += c;
        }
    }

    cout << total << endl;
    return 0;
}