-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_pi.py
More file actions
56 lines (40 loc) · 1.52 KB
/
python_pi.py
File metadata and controls
56 lines (40 loc) · 1.52 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Uses Python 3.x
from decimal import Decimal, getcontext
from time import time, strftime
import datetime
"""
Machin's Formula as:
4(4(arccot(5))-arccot(239))
or expanded as:
4(4(1/5 - 1/3(5)^3) + 1/5(5)^5 + 1/7(5)^7) - (1/239 - 1/3(239)^3 + 1/5(239)^5 + 1/7(239)^7))
of course a lot longer
"""
def arccot(x, digits):
# set precision and starting values
getcontext().prec = digits
total = 0
n = 1
# loop while new term is large enough to actually change the total
while Decimal((2 * n - 1) * x ** (2 * n - 1)) < Decimal(10 ** digits):
# find value of new term
term = ((-1)**(n - 1)) * 1 / Decimal((2 * n - 1) * x ** (2 * n - 1))
# add the new term to the total
total += term
# next n
n += 1
# return the sum
return total
# pi function
def pi(decimals):
# start timer
timestart = time()
# find pi using Machin's Formula pi = 4 * (4 * arccot(5) - arccot(239))
# and the power formula for arccot (see arccot function above)
print("pi = " + str(Decimal(4 * (4 * arccot(5, decimals + 3) - arccot(239, decimals + 3))).quantize(Decimal(10) ** (-decimals))))
# display elapsed time
timeelapsedint = round(time() - timestart, 2)
timeelapsedstr = str(datetime.timedelta(seconds = round(timeelapsedint, 0)))
print("runtime: " + timeelapsedstr + " or " + str(timeelapsedint) + " seconds.")
if __name__ == "__main__":
n = input("How many digits of pi would you like to know? ")
pi(int(n))