All Euler problems
Project Euler

Fermat-like Equations

Count tuples (a, b, c, e, f) with a^e + b^e = c^f, 0 < a < b, e >= 2, f >= 3, c^f <= N. Given: F(10^3) = 7, F(10^5) = 53, F(10^7) = 287. Find F(10^18).

Source sync Apr 19, 2026
Problem #0678
Level Level 30
Solved By 288
Languages C++, Python
Answer 1986065
Length 267 words
number_theorymodular_arithmeticbrute_force

Problem Statement

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

If a triple of positive integers \((a, b, c)\) satisfies \(a^2+b^2=c^2\), it is called a Pythagorean triple. No triple \((a, b, c)\) satisfies \(a^e+b^e=c^e\) when \(e \ge 3\) (Fermat’s Last Theorem). However, if the exponents of the left-hand side and right-hand side differ, this is not true. For example, \(3^3+6^3=3^5\).

Let \(a, b, c, e, f\) be all positive integers, \(0 < a < b\), \(e \ge 2\), \(f \ge 3\) and \(c^f \le N\). Let \(F(N)\) be the number of \((a, b, c, e, f)\) such that \(a^e+b^e=c^f\). You are given \(F(10^3) = 7\), \(F(10^5) = 53\) and \(F(10^7) = 287\).

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

Problem 678: Fermat-like Equations

Mathematical Analysis

Case Analysis by (e,f)(e, f)

The dominant cases are small ee and ff, since larger exponents yield fewer solutions:

Case (e=2,f=3)(e=2, f=3): a2+b2=c3a^2 + b^2 = c^3. This requires c3c^3 to be a sum of two squares, which (by Fermat’s theorem) means every prime p3(mod4)p \equiv 3 \pmod{4} dividing c3c^3 appears to an even power.

Theorem (Sum of Two Squares). nn is a sum of two squares iff every prime p3(mod4)p \equiv 3 \pmod{4} appears to an even power in the factorization of nn.

Case (e=2,f=5)(e=2, f=5): a2+b2=c5a^2 + b^2 = c^5. Similar analysis with c5c^5.

General: For each pair (e,f)(e, f), enumerate cc with cfNc^f \le N, check if cfc^f is a sum of ee-th powers.

Parameterization for e=2e = 2

When e=2e = 2: a2+b2=cfa^2 + b^2 = c^f. Using Gaussian integers: cf=z2c^f = |z|^2 where z=a+biz = a + bi. Factor cfc^f in Z[i]\mathbb{Z}[i] to find all representations.

The number of representations of nn as a2+b2a^2 + b^2 with a>0,b>0,a<ba > 0, b > 0, a < b is related to r2(n)/4r_2(n)/4 minus the diagonal.

Enumeration Bounds

For cfN=1018c^f \le N = 10^{18}:

  • f=3f = 3: c106c \le 10^6
  • f=4f = 4: c104.531623c \le 10^{4.5} \approx 31623
  • f=5f = 5: c103.63981c \le 10^{3.6} \approx 3981
  • f6f \ge 6: c103c \le 10^3

For each cc and ff, compute cfc^f and count representations as ae+bea^e + b^e.

Concrete Examples

(a,b,c,e,f)(a, b, c, e, f)ae+be=cfa^e + b^e = c^f
(3,6,3,3,5)(3, 6, 3, 3, 5)27+216=243=3527 + 216 = 243 = 3^5
(1,2,5,4,2)(1, 2, 5, 4, 2)?Need to verify

Small cases for F(103)=7F(10^3) = 7: enumerate all (a,b,c,e,f)(a,b,c,e,f) with cf1000c^f \le 1000.

Derivation

Editorial

We iterate over each f=3,4,,59f = 3, 4, \ldots, 59 (since 259<1018<2602^{59} < 10^{18} < 2^{60}). We then iterate over each e=2,3,e = 2, 3, \ldots (until 2e>M2^e > M). Finally, count pairs (a,b)(a, b) with ae+be=Ma^e + b^e = M, 0<a<b0 < a < b.

Pseudocode

For each $f = 3, 4, \ldots, 59$ (since $2^{59} < 10^{18} < 2^{60}$):
Compute $M = c^f$
For each $e = 2, 3, \ldots$ (until $2^e > M$):
Count pairs $(a, b)$ with $a^e + b^e = M$, $0 < a < b$
Sum all valid tuples

Counting Sum-of-Powers Representations

For ae+be=Ma^e + b^e = M with e3e \ge 3: since a<ba < b and ae<Ma^e < M, we have a<M1/ea < M^{1/e}. For each aa, check if MaeM - a^e is a perfect ee-th power.

For e=2e = 2: use the sum-of-two-squares decomposition via prime factorization in Z[i]\mathbb{Z}[i].

Proof of Correctness

The enumeration is exhaustive: all valid (e,f)(e, f) pairs are checked, and for each, all valid cc values are enumerated. The inner loop checks all possible aa values. No solutions are missed because the bounds on cc and aa are exact.

Complexity Analysis

  • Outer loop: f3N1/fN1/3+N1/4+=O(N1/3)\sum_{f \ge 3} N^{1/f} \approx N^{1/3} + N^{1/4} + \cdots = O(N^{1/3}).
  • Inner loop per (c,f)(c, f): For e=2e = 2, O(M)=O(cf/2)O(\sqrt{M}) = O(c^{f/2}) via Gaussian integer factorization. For e3e \ge 3, O(M1/e)=O(cf/e)O(M^{1/e}) = O(c^{f/e}).
  • Total: dominated by (e=2,f=3)(e=2, f=3) case: O(N1/3N1/2)=O(N5/6)O(N^{1/3} \cdot N^{1/2}) = O(N^{5/6})… but with much smaller constant.

In practice, for N=1018N = 10^{18}: 106\sim 10^6 values of cc for f=3f = 3, each taking O(1)O(1) with precomputed factorizations.

Answer

1986065\boxed{1986065}

Code

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

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

/*
 * Problem 678: Fermat-like Equations
 *
 * 1. Implement the mathematical framework described above.
 * 2. Optimize for the target input size.
 * 3. Verify against known test values.
 */


int main() {
    printf("Problem 678: Fermat-like Equations\n");
    // Enumerate perfect powers, Pythagorean-like parameterization for each (e,f) pair

    int N = 100;
    long long total = 0;
    for (int n = 1; n <= N; n++) {
        total += n; // Replace with problem-specific computation
    }
    printf("Test sum(1..%d) = %lld\n", N, total);
    printf("Full implementation needed for target input.\n");
    return 0;
}