The Pi Project

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”
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.


This entry was published on July 8, 2013 at 4:12 pm and is filed under Old Posts. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: