Skip to content

Instantly share code, notes, and snippets.

@catupper
Created May 20, 2011 08:47
Show Gist options
  • Save catupper/982580 to your computer and use it in GitHub Desktop.
Save catupper/982580 to your computer and use it in GitHub Desktop.
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