#CHUDNOVSKY's WITH GMPY
import gmpy
import time
#import math
from decimal import *
amountiterations = int(raw_input('Enter amount of iterations: '))
places = int(raw_input('Enter amount of precision you want: '))
def Chudnosky(amountiterations,precision):
gmpy.set_minprec(precision)
k = 0
total = 0
while k < amountiterations:
gmpy.set_minprec(precision)
sign = (-1)**(k%2)
middleterm = gmpy.fac(6*k)
endterm = 13591409 + 54514*k
numerator = sign*middleterm*endterm
dfirstterm = gmpy.fac(3*k)
dmiddleterm = (gmpy.fac(k))**3
dendterm = gmpy.fsqrt(gmpy.mpf(640320)**((6 * k) + 3))
denominator = gmpy.mpf(dfirstterm)*gmpy.mpf(dmiddleterm)*dendterm
div = gmpy.mpf(numerator) / denominator
#print "div: " + str(div)
total += div
#print "total: " + str(total)
k = k + 1
total *= 12
return (gmpy.mpf(1)/total)
start_time = time.time()
result = Chudnosky(amountiterations,places)
calc_time = time.time() - start_time
print(result)
f = open("pi.txt","r")
match = ""
correct = 0
for digit in str(result):
if digit == f.read(1):
match = match + "T"
if digit != ".":
correct = correct + 1
else:
match = match + "F"
print(match)
f.close()
#print "It took " + str(calc_time) + " seconds to calculate pi with " + str(correct) + "/" + str(len(str(result))) + " digits correct."
#END FILE
#CHUDNOVSKY's WITH DECIMAL
import time
import math
from decimal import *
digits = int(raw_input('Enter amount of digits: '))
def Chudnosky(digits):
iter = int(digits/10) + 1
getcontext().prec = digits*2
total = Decimal(0)
for k in range(0,iter+1):
sign = (-1)**(k%2)
middleterm = Decimal(math.factorial(6*k))
endterm = Decimal(13591409) + Decimal(54514*k)
numerator = sign*middleterm*endterm
dfirstterm = Decimal(math.factorial(3*k))
dmiddleterm = Decimal((math.factorial(k))**3)
base = Decimal(640320)
power = Decimal(6)*k + Decimal(3)
dendterm = Decimal.sqrt(base**power)
denominator = dfirstterm*dmiddleterm*dendterm
div = Decimal(numerator) / Decimal(denominator)
#print "div: " + str(div)
total += Decimal(div)
#print "total: " + str(total)
total *= 12
getcontext().prec = digits
return (Decimal(1)/Decimal(total))
start_time = time.time()
result = Chudnosky(digits)
calc_time = time.time() - start_time
print(Decimal(result))
f = open("pi.txt","r")
match = ""
correct = 0
for digit in str(result):
if digit == f.read(1):
match = match + "T"
if digit != ".":
correct = correct + 1
else:
match = match + "F"
print(match)
f.close()
#print "It took " + str(calc_time) + " seconds to calculate pi with " + str(correct) + "/" + str(len(str(result))) + " digits correct."
#END FILE
#Nilakantha with decimal
#need to figure out how to get moar precision
#import bigfloat
#import decimal
from decimal import *
import time
start_time = time.time()
x = 1
pi = Decimal(3)
add = True
iterations = int(raw_input('Enter amount of iterations: '))
places = int(raw_input('Enter amount of decimal places you wish to display: '))
getcontext().prec = places
while x <= iterations:
if add == True:
pi = pi + Decimal(4)/Decimal((2*x)*(2*x+1)*(2*x+2))
#print "add"
if add == False:
pi = pi - Decimal(4)/Decimal((2*x)*((2*x)+1)*((2*x)+2))
#print "minus"
add = not add
#print "this is an iteration"
x = x + 1
print(Decimal(pi))
calc_time = time.time() - start_time
f = open("pi.txt","r")
result = ""
correct = 0
for digit in str(Decimal(pi)):
if digit == f.read(1):
result = result + "T"
if digit != ".":
correct = correct + 1
else:
result = result + "F"
print(result)
f.close()
#check_time = time.time() - start_time - calc_time
print "It took " + str(calc_time) + " seconds to calculate pi with " + str(correct) + "/" + str(places) + " digits correct."