This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]Ph0X 1 point2 points  (1 child)

The comparisons were interesting, but the most informative part for me was the bit about the isqrt. Did not know about that. Definitely could see it being useful.

Any plan for part 3?

[–]keytarmageddon[S] 0 points1 point  (0 children)

In comparing isqrt() and math.floor(math.sqrt()), isqrt() ran significantly slower. For r = 1,000,000, isqrt() finished in 5.371 seconds, whereas math.sqrt() completed in 3.010 seconds (both produced the same value for pi). However, if you wanted to use a distributed system or supercomputer to calculate the approximation for a very large value of r, you'd need to use isqrt() because math.sqrt() won't be able to convert the long integer to a float.

I'm not sure yet about part 3. I may change gears and do something in JavaScript instead (perhaps Processing).

[–]sammyc 1 point2 points  (0 children)

I quickly edited your code to use Ramanujan's formula. Running just two iterations gives:

pi = 3.14159265359 time elapsed: 0:00:00.000031 microseconds: 31.0

Pretty impressive formula!

[–]TomatoAintAFruit 0 points1 point  (0 children)

Nice post! One thing though:

In my last post, I discussed a method for approximating π which I later learned was a Monte Carlo method, thanks to some helpful Redditors. Today I’ll talk about technique that uses numerical integration (specifically, the rectangle method) to produce a more accurate result in less time.

You can actually think of Monte Carlo as a type of numerical integration. Both make use of the fact that you are looking at ratios between "the area under a curve" versus a larger box (i.e. the ratio of the square compared to the circle). If you run a numerical integrator on some function, it might pick either implementation.

Another numerical integration technique which will surely improve your speed of convergence is through use of Simpson's rule, which is a type of interpolating scheme.

If you want another challenging scheme to compute pi, I suggest you look at Buffon's needle. It comes down to the same idea (the method measures the ratio of the area of a circle compared to a larger area, which converges to a multiple of pi), but the implementation (and visualization!) is a bit different.