Created
May 20, 2011 08:47
-
-
Save catupper/982580 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import division | |
import decimal | |
decimal.getcontext().prec = 100 | |
dd=decimal.Decimal | |
def newton(x): | |
xn=dd(0) | |
while True: | |
xn+=1 | |
if xn**2>x: | |
break | |
while True: | |
ss=xn | |
xn=(xn+x/xn)/2 | |
if ss-xn<((dd(1)/10)**100): | |
break | |
return xn | |
def pi2(x): | |
a=dd(1) | |
b=newton(dd("0.5")) | |
t=dd("0.25") | |
p=dd(1) | |
for sq in range(x): | |
X=(a+b)/dd(2) | |
t=t-p*((a-X)**2) | |
b=newton(a*b) | |
a=X | |
p*=2 | |
return ((a+b)**2)/(4*t) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment