# Archimedes Method for PI
# FB - 200912082
# Revised by Bjorn.madsen AT operationsresearchgroup.com for Python3.3
# BHM - 20130302
import decimal
def ArchPi(precision=99):
# x: circumference of the circumscribed (outside) regular polygon
# y: circumference of the inscribed (inside) regular polygon
decimal.getcontext().prec = precision+1
D=decimal.Decimal
# max error allowed
eps = D(1)/D(10**precision)
# initialize w/ square
x = D(4)
y = D(2)*D(2).sqrt()
ctr = D(0)
while x-y > eps:
xnew = 2*x*y/(x+y)
y = D(xnew*y).sqrt()
x = xnew
ctr += 1
return str((x+y)/D(2))
PiA=ArchPi(20)
print("pi =",PiA)
pi = 3.14159265358979323848
print(ArchPi(99))
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068