Find taxicab numbers in $O(n)$ time

I don't know about $O(n)$, but I can do it in $O(n^2)$. The main idea is to use initialization of an array in $O(1)$ (this is the best reference that I've found, which is weird since this seems like a very important concept). Then you iterate through all the possible $(a,\ b)$ pairs and do the same as step 1 in your proposed algorithm. Since $a^3+b^3 \leq 2n^3$, the array needs to be of size $2n^3$, but it's still initialized in $O(1)$. Accessing an array element is $O(1)$ like in a regular array.


Apparently this is a solved problem and every rational solution to $x^3 + y^3 = z^3 + w^3$ is proportional to

$x = 1 − (p − 3q)(p^2 + 3q^2)$

$y = −1 + (p + 3q)(p^2 + 3q^2)$

$z = (p + 3q) − (p^2 + 3q^2)^2$

$w = −(p − 3q) + (p^2 + 3q^2)^2$

See: http://129.81.170.14/~erowland/papers/koyama.pdf, Page 2.

Also see: http://sites.google.com/site/tpiezas/010 (search for J. Binet).

So it seems like an O(n^2) algorithm might be possible. Perhaps using this more cleverly can give us an O(n) time algorithm.

Hope that helps. Please do update us with the solution given by your professor.


For $O(n^2)$ (randomized) time, you can also use a hashtable of size $\Theta(n^2)$. Looking up will be constant time because the number of taxicab numbers is $O(n^2)$.