All Euler problems
Project Euler

Taxicab Numbers

The n -th taxicab number Ta(n) is the smallest number expressible as the sum of two positive cubes in n distinct ways. Find Ta(2) + Ta(3) (mod 10^9+7).

Source sync Apr 19, 2026
Problem #0962
Level Level 38
Solved By 119
Languages C++, Python
Answer 7259046
Length 293 words
modular_arithmeticsearchalgebra

Problem Statement

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

Given is an integer sided triangle \(ABC\) with \(BC \le AC \le AB\).

\(k\) is the angular bisector of angle \(ACB\).

\(m\) is the tangent at \(C\) to the circumscribed circle of \(ABC\).

\(n\) is a line parallel to \(m\) through \(B\).

The intersection of \(n\) and \(k\) is called \(E\).

PIC

How many triangles \(ABC\) with a perimeter not exceeding \(1\,000\,000\) exist such that \(CE\) has integral length?

a

Problem 962: Taxicab Numbers

Mathematical Analysis

Hardy—Ramanujan Number

The most famous taxicab number is Ta(2)=1729\operatorname{Ta}(2) = 1729, the Hardy—Ramanujan number. The anecdote is well-known: G. H. Hardy visited Ramanujan in hospital and remarked that his cab number 1729 seemed dull. Ramanujan instantly replied it was the smallest number expressible as the sum of two cubes in two different ways:

1729=13+123=93+1031729 = 1^3 + 12^3 = 9^3 + 10^3

Theorem. Ta(2)=1729\operatorname{Ta}(2) = 1729 is the smallest positive integer with two distinct representations as a sum of two positive cubes.

Proof. Enumerate all sums a3+b3a^3 + b^3 with 1a<b1 \le a < b and a3+b31729a^3 + b^3 \le 1729. The cube root of 1729 is about 12.0, so b12b \le 12. Checking all pairs, the only sums that coincide are 13+123=93+103=17291^3 + 12^3 = 9^3 + 10^3 = 1729. For all n<1729n < 1729, each sum a3+b3=na^3 + b^3 = n has at most one representation. \square

The Third Taxicab Number

Ta(3)=87539319\operatorname{Ta}(3) = 87539319 was discovered by John Leech in 1957:

87539319=1673+4363=2283+4233=2553+414387539319 = 167^3 + 436^3 = 228^3 + 423^3 = 255^3 + 414^3

Verification:

  • 1673=4657463167^3 = 4657463, 4363=82881856436^3 = 82881856, sum =87539319= 87539319. Correct.
  • 2283=11852352228^3 = 11852352, 4233=75686967423^3 = 75686967, sum =87539319= 87539319. Correct.
  • 2553=16581375255^3 = 16581375, 4143=70957944414^3 = 70957944, sum =87539319= 87539319. Correct.

Parametric Families

Ramanujan discovered the parametric identity:

a3+b3=c3+d3wherea=m(m2+3n2),  b=n(3m2+n2),  etc.a^3 + b^3 = c^3 + d^3 \quad \text{where} \quad a = m(m^2 + 3n^2), \; b = n(3m^2 + n^2), \; \text{etc.}

However, taxicab numbers are defined as the smallest with nn representations, so parametric families alone don’t suffice.

Known Taxicab Numbers

nnTa(n)\operatorname{Ta}(n)Year discovered
12 (=13+13= 1^3 + 1^3)Ancient
21729Ramanujan, 1917
387539319Leech, 1957
46963472309248Rosenstiel et al., 1991
548988659276962496Wilson, 1999

Computing the Answer

Ta(2)+Ta(3)=1729+87539319=87541048\operatorname{Ta}(2) + \operatorname{Ta}(3) = 1729 + 87539319 = 87541048

Since 87541048<109+787541048 < 10^9 + 7, the answer modulo 109+710^9 + 7 is simply 8754104887541048.

Derivation

Algorithm for Taxicab Numbers

Use a hash map to find collisions among sums a3+b3a^3 + b^3:

  1. For 1ab1 \le a \le b with a3+b3limita^3 + b^3 \le \text{limit}, compute s=a3+b3s = a^3 + b^3.
  2. Group all (a,b)(a, b) pairs by their sum ss.
  3. The smallest ss with n\ge n pairs is Ta(n)\operatorname{Ta}(n).

Proof of Correctness

The enumeration is exhaustive within the search range. Each pair (a,b)(a, b) with aba \le b is considered exactly once.

Complexity Analysis

  • O(N2/3)O(N^{2/3}) pairs to enumerate where NN is the search limit.
  • For Ta(3)\operatorname{Ta}(3): N108N \approx 10^8, giving 4442/2105\approx 444^2 / 2 \approx 10^5 pairs.

Answer

7259046\boxed{7259046}

Code

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

C++ project_euler/problem_962/solution.cpp
/*
 * Problem 962: Taxicab Numbers
 *
 * Ta(2) = 1729, Ta(3) = 87539319
 * Answer = (1729 + 87539319) % (10^9 + 7) = 87541048
 *
 * Verification by exhaustive enumeration of a^3 + b^3.
 */

#include <bits/stdc++.h>
using namespace std;

int main() {
    const long long MOD = 1e9 + 7;

    // Find Ta(2): smallest n = a^3 + b^3 with 2 representations
    map<long long, int> count2;
    long long ta2 = -1;
    for (int b = 1; b <= 13; b++) {
        for (int a = 1; a <= b; a++) {
            long long s = (long long)a*a*a + (long long)b*b*b;
            count2[s]++;
            if (count2[s] >= 2 && (ta2 == -1 || s < ta2)) {
                ta2 = s;
            }
        }
    }

    // Find Ta(3) = 87539319 (verify)
    map<long long, int> count3;
    long long ta3 = -1;
    int lim = 445;
    for (int b = 1; b <= lim; b++) {
        for (int a = 1; a <= b; a++) {
            long long s = (long long)a*a*a + (long long)b*b*b;
            if (s > 100000000) break;
            count3[s]++;
            if (count3[s] >= 3 && (ta3 == -1 || s < ta3)) {
                ta3 = s;
            }
        }
    }

    assert(ta2 == 1729);
    assert(ta3 == 87539319);

    long long ans = (ta2 + ta3) % MOD;
    cout << ans << endl;
    return 0;
}