# Calculate Pi with Python

Intro to Computer programming worked at calculating digits of pi today. The actual algorithms aren’t too bad, but getting more than the standard number of digits from a double is a bit trickier. Here’s a program that calculates pi using:

Bailey–Borwein–Plouffe formula Bellard’s formula and
Chudnovsky algorithm Holy smokes is Chudnovsky algorithm’s fast!

```			 Plouff 		 Bellard 			 Chudnovsky
Iteration number  1   3.133333333333333333333333   3.141765873015873015873017   3.141592653589734207668453
Iteration number  2   3.141422466422466422466422   3.141592571868390306374053   3.141592653589793238462642
Iteration number  3   3.141587390346581523052111   3.141592653642050769944284   3.141592653589793238462642
Iteration number  4   3.141592457567435381837004   3.141592653589755368080514   3.141592653589793238462642
Iteration number  5   3.141592645460336319557021   3.141592653589793267843377   3.141592653589793238462642
Iteration number  6   3.141592653228087534734378   3.141592653589793238438852   3.141592653589793238462642
Iteration number  7   3.141592653572880827785241   3.141592653589793238462664   3.141592653589793238462642
Iteration number  8   3.141592653588972704940778   3.141592653589793238462644   3.141592653589793238462642
Iteration number  9   3.141592653589752275236178   3.141592653589793238462644   3.141592653589793238462642
Iteration number  10   3.141592653589791146388777   3.141592653589793238462644   3.141592653589793238462642
Iteration number  11   3.141592653589793129614171   3.141592653589793238462644   3.141592653589793238462642
Iteration number  12   3.141592653589793232711293   3.141592653589793238462644   3.141592653589793238462642
Iteration number  13   3.141592653589793238154767   3.141592653589793238462644   3.141592653589793238462642
Iteration number  14   3.141592653589793238445978   3.141592653589793238462644   3.141592653589793238462642
Iteration number  15   3.141592653589793238461733   3.141592653589793238462644   3.141592653589793238462642
Iteration number  16   3.141592653589793238462594   3.141592653589793238462644   3.141592653589793238462642
Iteration number  17   3.141592653589793238462641   3.141592653589793238462644   3.141592653589793238462642
Iteration number  18   3.141592653589793238462644   3.141592653589793238462644   3.141592653589793238462642
Iteration number  19   3.141592653589793238462644   3.141592653589793238462644   3.141592653589793238462642```

## Source code (Python)

```from decimal import *

#Sets decimal to 25 digits of precision
getcontext().prec = 25

def factorial(n):
if n<1:
return 1
else:
return n * factorial(n-1)

def plouffBig(n): #http://en.wikipedia.org/wiki/Bailey%E2%80%93Borwein%E2%80%93Plouffe_formula
pi = Decimal(0)
k = 0
while k < n:
pi += (Decimal(1)/(16**k))*((Decimal(4)/(8*k+1))-(Decimal(2)/(8*k+4))-(Decimal(1)/(8*k+5))-(Decimal(1)/(8*k+6)))
k += 1
return pi

def bellardBig(n): #http://en.wikipedia.org/wiki/Bellard%27s_formula
pi = Decimal(0)
k = 0
while k < n:
pi += (Decimal(-1)**k/(1024**k))*( Decimal(256)/(10*k+1) + Decimal(1)/(10*k+9) - Decimal(64)/(10*k+3) - Decimal(32)/(4*k+1) - Decimal(4)/(10*k+5) - Decimal(4)/(10*k+7) -Decimal(1)/(4*k+3))
k += 1
pi = pi * 1/(2**6)
return pi

def chudnovskyBig(n): #http://en.wikipedia.org/wiki/Chudnovsky_algorithm
pi = Decimal(0)
k = 0
while k < n:
pi += (Decimal(-1)**k)*(Decimal(factorial(6*k))/((factorial(k)**3)*(factorial(3*k)))* (13591409+545140134*k)/(640320**(3*k)))
k += 1
pi = pi * Decimal(10005).sqrt()/4270934400
pi = pi**(-1)
return pi
print "\t\t\t Plouff \t\t Bellard \t\t\t Chudnovsky"
for i in xrange(1,20):
print "Iteration number ",i, " ", plouffBig(i), " " , bellardBig(i)," ", chudnovskyBig(i)

```

## 10 thoughts on “Calculate Pi with Python”

1. Montana Burr says:

Why not just integrate sqrt(1-x) using a Riemann sum & multiply the integral by 2?

Here’s my code:
import math
import time
def y(x,r):
square = x ** 2
return math.sqrt(r – square)
while 1:
n = input(“n “)
i = 0.00
cx = 2.00 / n
area = 0.00
oldTime = time.clock()
while i < n:
x = -1 + (cx * i)
area += y(x,1) * cx
i += 1
piCalc = 2.00 * area
newTime = time.clock()
timeDiff = newTime – oldTime
print piCalc
print "Calculation completed in " + str(timeDiff) + " seconds of CPU time"

2. Andy says:

Your integral doesn’t actually give you the nth digit of pi, it simply calculates pi with increasing accuracy as n gets larger. The limit of your summation as n approaches infinity is pi, but that doesn’t mean you are getting the nth digit of pi when you type in a value for n.

3. Farooq Karimi Zadeh says:

Hello.
You could use math module’s factorial function instead of defining your own. Also recursion is slower so you’d better write any function you want to write with iteration.
Thanks for the code!

4. pouya says:

i entered 1000 digits and it get to this value
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141273724587006606315588174881520920962829254091715364367892590360011330530548820466521384146951941511609433057270365759591953092186117381932611793105118548074462379962749567351885752724891227938183011949129833673362440656643086021394946395224737190702179860943702770539217176293176752384674818467669405132000568127145263560827785771342757789609173637178721468440901224953430146549585371050792279689258923542019956112129021960864034418159813629774771309960518707211349999998372978049951059731732816096318595024459455346908302642522308253344685035261931188171010003137838752886587533208381420617177669147303598253490428755468731159562863882353787593751957781857780532171226806613001927876611195909216420207

5. Regina says:

Look at mine(slooooow).

x = 1
pi = float(4)
count = 0
pi = 4
while x == 1:
count = count + 1
if (count % 2) == 0:
pi = pi + (4 / ((count * 2) + 1))
if (count % 2) != 0:
pi = pi – (4 / ((count * 2) + 1))
print(pi, count)

1. Daniel Chang says:

Printing is very slow, so you should make it print after the loop. You can make the while loop eventually end by using a for loop instead.

6. Daniel Chang says:

Printing is very slow, so you should make it print after the loop. You can make the while loop eventually end by using a for loop instead.

7. Evelia says:

I will subscribe to that writer and his future publications.
The post is well organized, reminded me about https://parcelandjourney.com/lab-report/. I see the author has
a real knack for this topic. I like that theme, and I am in constant search of new bits and the latest news.
I truly enjoyed that one, as it’s filled with interesting facts and it is a sort
of easygoing article. I spent only a few minutes reading, and because of
well-structured text, I know it completely. Thanks!