Learning Julia - Ramanujan Approximation for Pi

Hi all,

I’m new to Julia and am learning the ropes thanks to the MIT course and books like Think Julia. I’m working through a few simple problems to start with, and wanted some help with this one.

Implementing Ramanujan’s approximation for pi - it is essentially a serial computation that I’ve implemented using a loop in Julia. This approach returns Inf / 0.0 when I exceed max_k = 5.

Can someone help me with why it doesn’t work for cases where max_k > 5?

1 Like

Try looking at the results of the fact function as you increase n. You will see that something goes wrong.

This is due to the fact that standard Julia integers (Int64) have a maximum possible size that they can store. A solution to this is to use BigInt instead.

A better solution is to rewrite your code (if possible) so that you don’t actually need to compute the whole factorial.

7 Likes

Thanks, I did look through the fact() function and I have changed the types there and elsewhere to BigInt. It helped me push the thing to max_k = 5.

However, I realized that the results for this series converge very close to what we know to be pi even with k=3. Therefore going to k>5 may be unnecessary - but I’m still reading up on this.

Thanks for helping me with this question! :slight_smile: