Taxicab Numbers

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Thu Dec 27 21:31:50 EST 2007


On Thu, 27 Dec 2007 13:48:36 -0800, rgalgon wrote:

> I'm new to Python and have been putting my mind to learning it over my
> holiday break. I've been looking over the functional programming aspects
> of Python and I'm stuck trying to come up with some concise code to find
> Taxicab numbers (http://mathworld.wolfram.com/TaxicabNumber.html).
> 
> "Taxicab(n), is defined as the smallest number that can be expressed as
> a sum of two positive cubes in n distinct ways"
> 
> In Haskell something like this could easily be done with: cube x = x * x
> * x
> 
> taxicab n = [(cube a + cube b, (a, b), (c, d))
>             | a <- [1..n],
>               b <- [(a+1)..n],
>               c <- [(a+1)..n],
>               d <- [(c+1)..n],
>               (cube a + cube b) == (cube c + cube d)]
> 
> Output::
> *Main> taxicab 10
> []
> *Main> taxicab 12
> [(1729,(1,12),(9,10))]
> *Main> taxicab 20
> [(1729,(1,12),(9,10)),(4104,(2,16),(9,15))]


I think you have COMPLETELY misunderstood what the taxicab numbers are, 
and in particular note the meaning of the argument n. I suggest you re-
read the Mathworld page:

http://mathworld.wolfram.com/TaxicabNumber.html


and pay special attention to the examples given.

You seem to be treating n as the number to be split into the sum of 
cubes, but that's not what n is supposed to be. n is the number of 
distinct sums.

Your example of taxicab 12 is wrong. According to Mathworld, only the 
first five taxicab numbers are known, and the 6th is suspected but not 
proven. The 12th taxicab number should be the number which can be written 
as twelve distinct sums of two cubes, that is something like:



Note: there is a slightly different definition of taxicab number, which 
does not take an "n" argument. In that case, if you list the taxicab 
numbers in order from smallest to largest, the 12th is 110808, not 1729.


-- 
Steven



More information about the Python-list mailing list