Ramanujan & Python

Steven Bethard steven.bethard at gmail.com
Mon Sep 6 17:25:47 EDT 2004


 <barnesc <at> engr.orst.edu> writes:
> 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].

>>> from itertools import count
>>> def ramanujan(p):
...     hist = {}
...     for x in count(1):
...             for y in range(1, x+1):
...                     sum = x**p + y**p
...                     if sum in hist:
...                             return sum, hist[sum], (x, y)
...                     hist[sum] = (x, y)
...
>>> ramanujan(2)
(50, (5, 5), (7, 1))
>>> ramanujan(3)
(1729, (10, 9), (12, 1))
>>> ramanujan(4)
(635318657, (134, 133), (158, 59))

Cool.  =)

Steve




More information about the Python-list mailing list