Ramanujan Factorial Approximation (2012)
johndcook.com
johndcook.com
Ramanujan was of course an extremely talented mathematician but IMHO, there is an unnecessary cult of mystery about him. In particular, there is no mystery as to where this formula comes from: it is obtained from applying a standard technique to a standard formula. Also, it is just one instance, I’ll call it the “k = 6” instance, of a family of such formulae and I’m near-certain the “k = 2” case was known before Ramanujan (on phone or would double check).
In any case, the standard formula is just the series expansion for: n! / sqrt{2πn} (n/e)^n and the standard technique is to raise both sides of a series expansion to some power, k say, multiply out one side, and then take k-th roots again.
In this case we just need to calculate: (1 + 1/12n + 1/288n^2 - 139/51840n^3 + O(1/n^4))^6 = 1 + 1/2n + 1/8n^2 + 1/240n^3 + O(1/n^4)
After taking the 6th root again we multiply inside by 8n^3 (from the LHS) and you get Ramanujan’s formula.
You can remove the overflow problems by modifying the code so that it computes ln(x!) as
lnfact = .5ln(math.pi)+x(ln(x)-1)
lnfact += ln(((8x + 4)x + 1)*x + 1/30.)/6.
That’s easy. What about 100! Or 124! Obviously you could calculate that by multiplying all the numbers but that’s slow. A quicker way is to use Ramanujan’s formula which will give you an answer with much less calculation. The catch is that the answer may not be exactly correct, just a very good approximation. For many applications this is not an issue, and a faster calculation is more useful than a slightly more accurate answer.
What's true is that Ramanujan spent an absolute significant amount of time working on math problems of all kinds, and the vast majority of his work was simply incorrect. In fact one thing Hardy appreciated and found most extraordinary about Ramanujan was not what Ramanujan managed to get right, but the ways in which he managed to get things wrong since that kind of revealed some unique insight into how Ramanujan went about solving problems.
Of course that is never what is showcased about him, instead we get infamous stories such as how Ramanujan seemingly figured out in a matter of seconds that the number 1729 is the smallest number expressible as the sum of two cubes in two different ways. What the story leaves out is that Ramanujan did not just figure this out on the fly but had been working on trying to prove Fermat's Last Theorem for years and as part of that work he had come across the number 1729 since it's related to elliptic curves (the basis of which would eventually lead to a proof of Fermat's Last Theorem).
At any rate, none of this is intended to diminish Ramanujan in any way, on the contrary my hope is to celebrate the very hard work and dedication that Ramanujan put towards mathematics. He is not someone who just divined correct solutions to complex math problems out of thin air but instead he spent countless hours for decades on end devoted to his passion and persevered through much trial and error. The only things we see of his work are the things he got right, and to that end it may seem like his accomplishments are magic; the truth is that they were anything but.
I had heard this before, and I believed it. But I got convinced that that's not true by the likes of George E. Andrews and Ken Ono. They say that most of his work is correct. All of this is still hearsay as far as I'm concerned. I'm not qualified to verify his math.
I thought it wasn't that 1729 was related to elliptic curves and the proof, but that it was a "near-miss" candidate in a search for finding a counterexample in order to disprove the theorem. And 9³ + 10³ = 12³ + 1 = 1729 was the smallest of many of these near-miss candidates that Ramanujan had found.
Was that not right?
My impression is just opposite. Do you have any reference proving that vast majority of his work was simply incorrect.
"What did Ramanujan get wrong?" - https://mathoverflow.net/questions/288410/what-did-ramanujan...
"An Overview of Ramanujan's Notebooks" - https://faculty.math.illinois.edu/~berndt/articles/aachen.pd...
From Robert Kanigel's The Man Who Knew Infinity [0].
My understanding is that Ramanujan did most of his derivations on slate and tended to preserve only the results on paper. His insight was tremendous, but it was backed up by a tremendous amount of elbow work.
[0] https://en.wikipedia.org/wiki/The_Man_Who_Knew_Infinity_(boo...
For a comparison on the other end our spatial memory structures in the brain have changed since widespread use of gps/mapping apps, apparently.
Edit after googling: the gamma function not the solution to a differential equation, in fact there is a proof that it cant be [*] but it's formulated as an integral, whose approximation could lead to odd looking equations like the one in the article
Why not just use
fact = math.floor(fact)
instead of int()?I hate functions that randomly decides to return a different type too.
The real problem is that `fact` is not the actual factorial but its approximation (doubly so because it goes through float anyway). So all extra precision provided by bigint is useless.
Doubly so for anything Ramanujan.
You compute n^n the same way you compute a^n which is to say that a^n = e^(n x log(a))
To be fair, I’m not sure whether similar optimizations exist for computing the factorial, but I don’t think so.
Permutation facts:
https://jugad2.blogspot.com/2016/10/by-vasudev-ram-nicomachu...
(The post URL does not match the post title, due to some error by me, which I forget. But the URL is the right one for the post.)