-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpiempython.py
More file actions
33 lines (24 loc) · 722 Bytes
/
Copy pathpiempython.py
File metadata and controls
33 lines (24 loc) · 722 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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))
if __name__ == '__main__':
print(ArchPi(99))