How to find all taxicab numbers less than N?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
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.
Related reading
- How to find all vertex-disjoint paths in a graph?
- How to find common strings among two very large files?
- How to find convex hull in a 3 dimensional space
- How to find cycles of a given length in a directed graph? Using networkx
- How to find if the second hand of a clock lies in the larger area or smaller one
- How to find integer nth roots?
- How to find if there are n consecutive set bits in a 32 bit buffer?
- How to find ith item in zigzag ordering?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.