The other day I decided to calculate pi, with as many decimal points as possible. The first step was to find the code to do this. Since most of my programing skills are in python so I decided to it in python. this is the code I found. It is quite fast running 1000 decimal points in less then half a second, half a minute for 10000. As you can see by those times the time curve is far from liner so calculating more decimals is going to take a long time. To make the output easier to read I added to the code so it outputs to a file. Then I set the decimal points to 100,000 and waited. I did not have a timer on it so I’m not sure exactly how long it took but I guessed about a hour and a half. You can the see the output file here.After that I added code to time the calculation. I will post time for longer calculations soon. Here is the python code:

import time

from decimal import Decimal, getcontext

start_time = time.clock()

f = open(“pi.txt”, “w”)

getcontext().prec = 10000

one = Decimal(1)

sixteen = Decimal(16)

pi = 0

k = Decimal(0)

while k <= 100:

pi += sixteen**(-k)*(4/(8*k+1)-2/(8*k+4)-1/(8*k+5)-1/(8*k+6))

k += 1

pi_string = str(pi)

print “file”

f.write(pi_string)

f.close()

print “done”

print “it took: “, float(time.clock()) – float(start_time), “seconds”

Also if anyone can run this code please tell my your time in comments, I would like to see different computers effect the times thanks.

Sebastien-

### Like this:

Like Loading...

*Related*