How to find all taxicab numbers less than N?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In mathematics, a taxicab number or Hardy–Ramanujan number is the smallest number that can be expressed as a sum of two positive cubes in n
different ways. The name originates from a famous anecdote involving mathematicians G.H. Hardy and Srinivasa Ramanujan regarding the number 1729, which is the smallest number that can be expressed as the sum of two cubes in two different ways:
This article delves into finding all taxicab numbers less than a given number N
, exploring algorithms and methods, while providing examples and a summary table.
Problem Definition
Given a number N
, our objective is to identify all taxicab numbers less than N
. The basic requirement is to compute sums of two different cubes in multiple ways and identify the smallest number which satisfies this condition for various (a, b)
pairs where a
and b
are positive integers.
Algorithm Approach
To find the taxicab numbers, we can leverage brute force computations or more optimized approaches using computational geometry or advanced data structures. Below, we discuss a more systematic algorithmic method:
Algorithm Steps
- Initialize a Limit: Since
max(a^3, b^3)must be less thanN, computeL = N^\{1/3\}to set upper bounds foraandb. - Utilize a Map Data Structure: Use a dictionary (or a hashmap) to track integer sums. Keys represent the sum
x = a^3 + b^3, and associated values are lists of pairs(a, b)that generate the key sum. - Iterate Over Potential Cubes:
- Loop over
aandbsuch that . - Calculate
s = a^3 + b^3. - If
s < N, check ifsis present in the map:- If present, append the pair
(a, b)to the list associated withs. - If not, initiate a new list with
(a, b).
- Extract Taxicab Numbers: Traverse the map and identify numbers that can be formed in multiple ways. This means having more than one pair
(a, b)forx.
Here's a sketch in Python for clarity:
- 1729: represented as and .
- Optimization: Although the approach loops over
aandbquadratically, it can be optimized using symmetry since . Hence, only pairs wherea <= bneed to be computed. - **Large
N**: WhenNis significantly large, computational methods such as parallel processing or using GPUs can be employed to speed up cube sum calculations.

