# Ways to Calculate PI (again)

**URL:** https://boards.straightdope.com/t/ways-to-calculate-pi-again/156568
**Category:** Factual Questions
**Created:** [February 21, 2003, 5:30pm UTC](https://boards.straightdope.com/t/ways-to-calculate-pi-again/156568 "2003-02-21T17:30:41Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![murphyboy99](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@murphyboy99](https://boards.straightdope.com/u/murphyboy99)
#### Post date: [February 21, 2003, 5:30pm UTC](https://boards.straightdope.com/t/ways-to-calculate-pi-again/156568/1 "2003-02-21T17:30:41Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Orbifold](https://avatars.discourse-cdn.com/v4/letter/o/779978/32.png) [@Orbifold](https://boards.straightdope.com/u/Orbifold)
#### Post date: [February 21, 2003, 6:32pm UTC](https://boards.straightdope.com/t/ways-to-calculate-pi-again/156568/2 "2003-02-21T18:32:21Z")

</div>

[http://mathworld.wolfram.com/PiFormulas.html](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.

---

<div class="post-metadata">

### Author: ![Keweenaw](https://avatars.discourse-cdn.com/v4/letter/k/da6949/32.png) [@Keweenaw](https://boards.straightdope.com/u/Keweenaw)
#### Post date: [February 21, 2003, 6:43pm UTC](https://boards.straightdope.com/t/ways-to-calculate-pi-again/156568/3 "2003-02-21T18:43:16Z")

</div>

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

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

---

<div class="post-metadata">

### Author: ![Tusculan](https://avatars.discourse-cdn.com/v4/letter/t/ecd19e/32.png) [@Tusculan](https://boards.straightdope.com/u/Tusculan)
#### Post date: [February 21, 2003, 9:28pm UTC](https://boards.straightdope.com/t/ways-to-calculate-pi-again/156568/4 "2003-02-21T21:28:00Z")

</div>

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]= 2_X[k]-a_X[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.  
");

---

<div class="post-metadata">

### Author: ![Tusculan](https://avatars.discourse-cdn.com/v4/letter/t/ecd19e/32.png) [@Tusculan](https://boards.straightdope.com/u/Tusculan)
#### Post date: [February 22, 2003, 3:50pm UTC](https://boards.straightdope.com/t/ways-to-calculate-pi-again/156568/5 "2003-02-22T15:50:28Z")

</div>

All those **û** should read SQRT.

---

<div class="post-metadata">

### Author: ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)
#### Post date: [February 22, 2003, 4:03pm UTC](https://boards.straightdope.com/t/ways-to-calculate-pi-again/156568/6 "2003-02-22T16:03:02Z")

</div>

The spigot algorithms are my favorite too.

---

<div class="post-metadata">

### Author: ![Fuji\_Kitakyusho](https://avatars.discourse-cdn.com/v4/letter/f/f0a364/32.png) [@Fuji\_Kitakyusho](https://boards.straightdope.com/u/Fuji_Kitakyusho)
#### Post date: [February 22, 2003, 6:09pm UTC](https://boards.straightdope.com/t/ways-to-calculate-pi-again/156568/7 "2003-02-22T18:09:11Z")

</div>

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…

---

<div class="post-metadata">

### Author: ![JRootabega](https://avatars.discourse-cdn.com/v4/letter/j/8e7dd6/32.png) [@JRootabega](https://boards.straightdope.com/u/JRootabega)
#### Post date: [February 22, 2003, 6:16pm UTC](https://boards.straightdope.com/t/ways-to-calculate-pi-again/156568/8 "2003-02-22T18:16:29Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Una\_Persson](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/una_persson/32/346_2.png) [@Una\_Persson](https://boards.straightdope.com/u/Una_Persson)
#### Post date: [February 22, 2003, 11:26pm UTC](https://boards.straightdope.com/t/ways-to-calculate-pi-again/156568/9 "2003-02-22T23:26:31Z")

</div>

> [@](#):
>
> \*Originally posted by TTT \*  
> **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).**

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](mailto:una_persson@hotmail.com) .

Una

---

<div class="post-metadata">

### Author: ![Tusculan](https://avatars.discourse-cdn.com/v4/letter/t/ecd19e/32.png) [@Tusculan](https://boards.straightdope.com/u/Tusculan)
#### Post date: [February 23, 2003, 10:56am UTC](https://boards.straightdope.com/t/ways-to-calculate-pi-again/156568/10 "2003-02-23T10:56:44Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![murphyboy99](https://avatars.discourse-cdn.com/v4/letter/m/7ba0ec/32.png) [@murphyboy99](https://boards.straightdope.com/u/murphyboy99)
#### Post date: [February 23, 2003, 11:14am UTC](https://boards.straightdope.com/t/ways-to-calculate-pi-again/156568/11 "2003-02-23T11:14:45Z")

</div>

Hi TTT, could I have a copy too please ([imurphy@link.co.uk](mailto: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.

---

<div class="post-metadata">

### Author: ![Tusculan](https://avatars.discourse-cdn.com/v4/letter/t/ecd19e/32.png) [@Tusculan](https://boards.straightdope.com/u/Tusculan)
#### Post date: [February 23, 2003, 1:15pm UTC](https://boards.straightdope.com/t/ways-to-calculate-pi-again/156568/12 "2003-02-23T13:15:13Z")

</div>

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

---

<div class="post-metadata">

### Author: ![rjk](https://avatars.discourse-cdn.com/v4/letter/r/ed655f/32.png) [@rjk](https://boards.straightdope.com/u/rjk)
#### Post date: [February 23, 2003, 7:53pm UTC](https://boards.straightdope.com/t/ways-to-calculate-pi-again/156568/13 "2003-02-23T19:53:18Z")

</div>

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

Thanks.

---

<div class="post-metadata">

### Author: ![Trigonal\_Planar](https://avatars.discourse-cdn.com/v4/letter/t/0ea827/32.png) [@Trigonal\_Planar](https://boards.straightdope.com/u/Trigonal_Planar)
#### Post date: [February 23, 2003, 8:47pm UTC](https://boards.straightdope.com/t/ways-to-calculate-pi-again/156568/14 "2003-02-23T20:47:59Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Keweenaw](https://avatars.discourse-cdn.com/v4/letter/k/da6949/32.png) [@Keweenaw](https://boards.straightdope.com/u/Keweenaw)
#### Post date: [February 23, 2003, 9:31pm UTC](https://boards.straightdope.com/t/ways-to-calculate-pi-again/156568/15 "2003-02-23T21:31:28Z")

</div>

murphyboy99 said

> [@](#):
>
> 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.

this site says [39 digits](http://personal.bgsu.edu/~carother/pi/Pi1.html)

I remember hearing that before somewhere.

Yes I agree, math is fun

---

<div class="post-metadata">

### Author: ![N9IWP](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/n9iwp/32/3154_2.png) [@N9IWP](https://boards.straightdope.com/u/N9IWP)
#### Post date: [February 23, 2003, 11:34pm UTC](https://boards.straightdope.com/t/ways-to-calculate-pi-again/156568/16 "2003-02-23T23:34:24Z")

</div>

I like [http://mathworld.wolfram.com/Bailey-Borwein-PlouffeAlgorithm.html](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](http://www.amazon.com/exec/obidos/ASIN/0312381859/weisstein-20/104-5015702-8569523)

---

<div class="post-metadata">

### Author: ![Tusculan](https://avatars.discourse-cdn.com/v4/letter/t/ecd19e/32.png) [@Tusculan](https://boards.straightdope.com/u/Tusculan)
#### Post date: [February 24, 2003, 8:05pm UTC](https://boards.straightdope.com/t/ways-to-calculate-pi-again/156568/17 "2003-02-24T20:05:27Z")

</div>

> [@](#):
>
> \*Originally posted by rjk \*  
> \*\*Having just come in, I realize it’s extra trouble, but could you send me a copy too?
> 
> Thanks. \*\*

‘Done.’ (random BtvS quote).
