Ways to Calculate PI (again)

Whoops, I was a little too eager then.

What’s the best way to calculate Pi ? I know of 2 methods,

1, The Leibniz Series (1 - 1/3 + 1/5 - 1/7…) converges to pi/4

2, The Monte Carlo Method, imagine a circle of radius 1 enclosed in a square of side 2. If you pick a point at random inside the square, the chances of getting it inside the circle are

area of circle/area of square = pi/4

So if you do it many times, the ratio of points inside the circle to total number of points approaches pi/4

I’ve tried both methods and the Leibniz series tends to converge faster, but I like the Monte Carlo method more because you can see what’s going on.

So, which method converges fastest, and which method do you think is coolest (Is it wrong to think of this as cool) ?

When come back, bring Pi.

http://mathworld.wolfram.com/PiFormulas.html contains more formulas than you ever believed necessary, including some (such as Ramanujan’s formulas starting at equation 60 , and Borwein’s algorithm starting at equation 98) which converge at positively ridiculous speeds.

As far as coolness goes, I find the spigot algorithms (which can calculate any given hexadecimal digit of pi without knowing the previous digits) to be pretty darn cool.

The Machin method was an improvement on Leibniz for faster convergence.

pi/4 = 4 * arctan (1/5) - arctan (1/239)

Following up on Orbifold, I can confirm that Borweins algorithm works quite fast in practice. Years ago when I still had mathematical aspirations, I implemented it in a small C program. If you’re interested I can mail it (or even post it: it is not that large). Here is the ‘descriptive’ part of the program. Even though I didn’t use Fast Fourier Transformations for multiplications it was still reasonably quick on an old XT (in the old days of 5 1/4 disks, when 8 MHz was considered a fast PC). The nice thing was that you could see the number of correct digits double with each iteration.

I’m afraid I don’t even understand my own program anymore.


printf("
This program calculates PI. It uses Borweins Algorithm, and works as follows:
");
printf("Consider three series of numbers, A, B and P.
");
printf("The series are recursive defined as follows:

");
printf("A[0]=û2, B[0]=0, P[0]=2+û2

");
printf("A[k+1]=(ûA[k]+(1/ûA[k]))/2
");
printf("B[k+1]=( ((ûA[k])*(1+B[k])) / (A[k]+B[k]) )
");
printf("P[k+1]=P[k]B[k+1](1+A[k+1])/(1+B[k+1])

");
printf("Now P[ì] = PI. This algorithm is quadratically convergent, which means that
");
printf("the number of correct digits rougly doubles with each iteration.
");
printf("For the calculation of the reciprocal 1/a and the square root ûa two
");
printf("selfcorrecting, quadratically convergent iterations are used:

");
printf("X[k+1]= 2X[k]-aX[k]*X[k], converges to 1/a.
");
printf("X[k+1]= (X[k]+(a/X[k]))/2, converges to ûa.

");
printf("For more details on the algorithm, see Math. Comp January 1988, vol.50,
no. 188, p. 283-286, article by D.H.Bailey.
");
printf("The iterations can be ‘seen’ by choosing mode 2. rN is the Nth iteration of
");
printf("the reciprocal, sqN is the Nth iteration of the squareroot. In mode 1 only
");
printf("only A and B are printed besides PI.
");

All those û should read SQRT.

The spigot algorithms are my favorite too.

What accuracy do you need it to? What computer system are you running your calculations on? What, exactly, are you doing?

pi=3.1415926535897932384626433832795028841971693993751058209…

If you need more decimal places than that, you’re obviously doing something over huge (astronomical) distances. If your application is that critical, perhaps the SDMB isn’t the best place to search for assistance…

Perhaps he’s curious and seeking knowledge for its own sake. Why discourage that?

Actually, TTT, I’m working on the next alpha release of my freeware science multi-tool. If you don’t mind sharing, I would be interested in seeing your code, and putting it into my program.

I can be mailed at una_persson@hotmail.com .

Una

Just mailed it, Anthracite. For what it’s worth, some additional comments. I’ve been sparse in commenting the program but it is really not so complicated. I used very basic methods for calculating multiple digits, using an array of (I think) double digits, i.e. every list item represents two digits. I cannot guarantee the correctness of the outcome, but when I checked the results with Pi-tables they seemed to turn out allright. If you’ve got other questions I’ll be happy to try to answer them, although I’m not sure whether I can still provide you with sufficient info on my younger self’s thought processes.

Enjoy!

Hi TTT, could I have a copy too please (imurphy@link.co.uk). I’m sure it’ll work better than my cobbled together Excel version of the Monte Carlo method.

Fuji Kitakyusho, I think you only need 15 digits to calculate the circumfrance of the universe to within a hydrogen atom. But maths is fun.

No problemo. The files are in the mail. Glad to see that my age-old program may still be of use to others.

Having just come in, I realize it’s extra trouble, but could you send me a copy too?

Thanks.

I’ve memorized ~~400 digits. That’s how I calculate pi :slight_smile:

murphyboy99 said

this site says 39 digits

I remember hearing that before somewhere.

Yes I agree, math is fun

I like http://mathworld.wolfram.com/Bailey-Borwein-PlouffeAlgorithm.html because you can get the 123,456,789,012,345 digit (hexit?) without getting all the previous ones.

Brian
see also http://www.amazon.com/exec/obidos/ASIN/0312381859/weisstein-20/104-5015702-8569523

‘Done.’ (random BtvS quote).