[Hardy about Ramanujan]: I remember once going to see him when he was ill at Putney. I had ridden in taxi cab number 1729 and remarked that the number seemed to me rather a dull one, and that I hoped it was not an unfavourable omen. "No," he replied, "it is a very interesting number; it is the smallest number expressible as the sum of two cubes in two different ways."
The two different ways are 1³ + 12³ and 9³ + 10³
I'm writing a series of functions (in C) to calculate different things related to Ramanujan's numbers. I'm now trying to write a function that returns the i-th Ramanujan's number. Since I've already created a function that checks whether a number is a Ramanujan number or not, the easy way would be to check every number, from 0 to infinity. If a given number is a Ramanujan number, increment a counter by one. Once the counter equals the index I'm looking for, I return the number. In code:
unsigned long ramanujan_index (unsigned long x, int counter, int index)
{
if (counter == index)
return x - 1;
if (is_ramanujan(x))
return ramanujan_index(x + 1, counter + 1, index);
else
return ramanujan_index(x + 1, counter, index);
}
It works, sure, but I'm a little worried that it's not as efficient as it could possibly be. Checking every number doesn't seem like the best solution. More so if we consider the first number is 1729, and the second is 4104. It seems that it'd take quite a lot of steps to find the 5th Ramanujan number (32832 steps, actually, since it has to check every number from 0 to 32832, which is the 5th number). Is there a better way to do so?