Calculations of PI

511 days ago by pekra

print "SAGE :", numerical_approx(pi,400) print print "Calculation of 4 digits of PI" # Wallis t = cputime() m=8477 x=2 for n in range(1,m+1): x = x * (2*(n)*2*(n))/(((2*n)+1)*((2*n)-1)) print "Wallis 1 :", numerical_approx(x), "CPU:", cputime(t) # Wallis 2 t = cputime() m=8477 y=(2**(4*m))*((factorial(m))**4) z=((factorial(2*m))**2)*(m+1/2) x=y/z print "Wallis 2 :",numerical_approx(x), "CPU:", cputime(t) #Euler t = cputime() m=10308 x=0 for n in range(1,m): x = x + 1/(n**2) x = sqrt(6*x) print "Euler :",numerical_approx(x), "CPU:", cputime(t) #Leibniz t = cputime() m=10828 x=0 for n in range(0,m): x = x + (-1)**n/(2*n+1) x = 4*x print "Leibniz :",numerical_approx(x), "CPU:", cputime(t) print print "Calculation of more digits of PI" # Vieta t = cputime() m=15 x=2 for n in range(1,m): x = 2 + sqrt(x) x = (2**(m+1))*sqrt(2-sqrt(x)) print "Vieta :",numerical_approx(x), "CPU:", cputime(t) print #Ramanujan t = cputime() m=14 x=0 for n in range(0,m): x = x+(factorial(4*n)*(1103+26390*n))/((factorial(n)**4)*(396**(4*n))) x = 9801/(x*sqrt(8)) print "Ramanujan:",numerical_approx(x,400), "CPU:", cputime(t) #Lambert t = cputime() m=150 x=1 for nn in range(1,m): n = m - nn x = (2*n-1)+(n**2)/x x = 4/x print "Lambert :",numerical_approx(x,400), "CPU:", cputime(t) #BBP t = cputime() m=90 x=0 for n in range(0,m): x = x+(1/(16**n))*((4/((8*n)+1))-((2/((8*n)+4))+(1/((8*n)+5))+(1/((8*n)+6)))) print "BBP :",numerical_approx(x,400), "CPU:", cputime(t) 
       
SAGE     :
3.1415926535897932384626433832795028841971693993751058209749445923078164\
0628620899862803482534211706798214808651328230665

Calculation of 4 digits of PI
Wallis 1 : 3.14150000993513 CPU: 0.16
Wallis 2 : 3.14150000993513 CPU: 0.02
Euler    : 3.14150000806581 CPU: 0.53
Leibniz  : 3.14150030043151 CPU: 0.1

Calculation of more digits of PI
Vieta    : 3.14159260737572 CPU: 0.01

Ramanujan:
3.1415926535897932384626433832795028841971693993751058209749445923078164\
0628620899862803482534211706798214808651372158654 CPU: 0.01
Lambert  :
3.1415926535897932384626433832795028841971693993751058209749445923078164\
0628620899862803482534211706798214808651325682942 CPU: 0.0
BBP      :
3.1415926535897932384626433832795028841971693993751058209749445923078164\
0628620899862803482534211706798214808651326929119 CPU: 0.0
SAGE     : 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230665

Calculation of 4 digits of PI
Wallis 1 : 3.14150000993513 CPU: 0.16
Wallis 2 : 3.14150000993513 CPU: 0.02
Euler    : 3.14150000806581 CPU: 0.53
Leibniz  : 3.14150030043151 CPU: 0.1

Calculation of more digits of PI
Vieta    : 3.14159260737572 CPU: 0.01

Ramanujan: 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651372158654 CPU: 0.01
Lambert  : 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651325682942 CPU: 0.0
BBP      : 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651326929119 CPU: 0.0