# Ramanujan & Python

Discussion in 'Python' started by barnesc@engr.orst.edu, Sep 6, 2004.

1. ### Guest

A bit of fun:

Ramanujan, an Indian mathematician, was once visited in the hospital
by G. H. Hardy, a prominant English mathematician. Hardy remarked
that he had taken taxi number 1729, and Ramanujan quickly replied
that 1729 is remarkable, as it is the smallest integer that can be
represented in two ways by the sum of two cubes: 1729 = 1**3 + 12**3
= 9**3 + 10**3 [1].

Spectacular, no?

The inspired reader checks this with a quick Python program:

>>> L = range(1,21)
>>> sums = [x**3+y**3 for x in L for y in L]
>>> histo = [sums.count(i) for i in range(max(sums)+1)]
>>> histo.index(4)

1729

Explanation:
20**3 is 8000, so our search extends only up to 8000.
sums contains x**3+y**3 values for each x in L for each y in L.
(note that 1**3 + 12**3 and 12**3 and 1**3 are counted twice in sums).
histo counts how many times each value appears in sums.

The point is that Python is great for mathematical experiments.

This particular program is inefficient on the histo= line, so if you
want to try other cases (such as smallest integer that can be
represented in two ways by the sum of two fourths), you'll need to
optimize.

[1]. http://scienceworld.wolfram.com/biography/Ramanujan.html

This document is in the public domain.

- Connelly

, Sep 6, 2004

2. ### Istvan AlbertGuest

wrote:

> Spectacular, no?

Not really.

I'm might sound a bit of off topic here I but I consider both of
these as 'cheapo' tricks that only sound good on paper (and to
reporters): 'Look Ma the man can compute the sum of two cubes
in a flash'

1**3 is 1, all he needed to know is that 12**3 is 1728
Similarly knowing that 10**3 is 1000 is trivial, all he
needed to know is that 9**3 is 729.

Just as with squared values whenever one works with cubes
they will tune very quickly in and start recognize the first
20 or so by heart in a matter of minutes...

Istvan.

Istvan Albert, Sep 6, 2004

3. ### Paul RubinGuest

Istvan Albert <> writes:
> 1**3 is 1, all he needed to know is that 12**3 is 1728
> Similarly knowing that 10**3 is 1000 is trivial, all he
> needed to know is that 9**3 is 729.
>
> Just as with squared values whenever one works with cubes
> they will tune very quickly in and start recognize the first
> 20 or so by heart in a matter of minutes...

There's another part to the question. Is there a smaller number than
1729, that's also the sum of two cubes in two distinct ways? Ramanujan
could answer that second question immediately. Can you?

Paul Rubin, Sep 6, 2004