Today, we will discuss how to find the two Hardy-Ramanujan numbers using Python.
A little prologue: The Mathematician Srinivasa Ramanujan was sick in a hospital. His friend G.H. Hardy visited him by going in a taxi with number 1729. Hardy said that 1729 is a dull number, and wished he did not get an omen from it. Ramanujan said that it is not dull, because it is the smallest four digit number that can be represented with two different sum of cubes:
Such numbers are called Hardy-Ramanujan numbers, also known as Taxicab numbers.
It turns out, there are not one, but two such four digit numbers, one of them is 1729. How do we find the other?
Well, we first need a variable to store all the numbers that are generated. We can also optionally create two variables to be used with the for loop, although that is not necessary:
we now need to use two for loops. What would the range be? Because we only need four digits, we can set it up from one to twenty one (twenty two cubed is 10648). in the first for loop, we set a=i**3. the ** operand is a power operand. In the next for loop, the nested for loop, we have the range set between i and 21, because we already visited numbers before i.
We do the same thing with b like we did with a, set it to j**3. After that, we append the sum of a and b:
Now that we have all the four digit numbers that can be made with the sum of two cubed numbers, we now need to figure out which one is the Taxicab number. We can use a for loop (and the function count()) to find it. In the for loop, we check if the current element we are on occurs two times. If so, it means that that number has two sums of two cubes, so it is a Taxicab number. Once we find one, we print it out:
Here is the full program and the output:
We now know the two Taxicab numbers, namely, 1729 and 4104.