Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: cp1252 -*-
- from mpmath import *
- import time
- dps = input("Number of decimals to compute: ")
- if dps < 11:
- print "Choosing 11 digits"
- dps = 11
- mp.dps = dps
- mp.dps=mp.dps+1
- a = 1
- x = 1
- b = mpf(1/mp.sqrt(2))
- c = 0.25
- i = 0
- y = 1
- derivedbefore = mpf(0)
- breakyn = False
- run = True
- #print "a =", a
- #print "x =", x
- #print "b =", b
- #print "c =", c
- #print "i =", i
- #print "Derived before:", derivedbefore
- start = time.clock()
- print "Start:", start
- print "------------------------------------------------------"
- print "------------------------------------------------------"
- while run == True:
- i = i+1
- y = mpf(a)
- a = mpf((a+b)/2)
- b = mpf(mp.sqrt(b*y))
- c = mpf(c-x*(mp.power(a-y,2)))
- x = mpf(2*x)
- derived = mpf(mp.power((a+b),2)/(4*c))
- if str(derived)[-10:-1] == str(derivedbefore)[-10:-1]:
- break
- print "Iteration:", i
- #print
- #print "Computed:", derived
- derivedbefore = derived
- print "------------------------------------------------------"
- stop=time.clock()
- print "------------------------------------------------------"
- print "Pi computed to ", mp.dps-1, "decimals:"
- print
- print derived
- duration=stop-start
- print "------------------------------------------------------"
- print "Time of execution: ", duration, "Seconds."
- print "Happy Pi Day!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement