Skip to content

Commit ef88c96

Browse files
Merge pull request #309 from MonliH/patch-1
Added a calculate PI function
2 parents 2b250c9 + 2ac5c9f commit ef88c96

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

primelib/primelib.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,25 @@
3838
goldbach(number) // Goldbach's assumption
3939
4040
"""
41+
def pi(maxK=70, prec=1008, disp=1007):
42+
"""
43+
maxK: nuber of iterations
44+
prec: precision of decimal places
45+
disp: number of decimal places shown
46+
"""
47+
from decimal import Decimal as Dec, getcontext as gc
48+
gc().prec = prec
49+
K, M, L, X, S = 6, 1, 13591409, 1, 13591409
50+
for k in range(1, maxK+1):
51+
M = Dec((K**3 - (K<<4)) * M / k**3)
52+
L += 545140134
53+
X *= -262537412640768000
54+
S += Dec(M * L) / X
55+
K += 12
56+
pi = 426880 * Dec(10005).sqrt() / S
57+
pi = Dec(str(pi)[:disp])
58+
return pi
59+
4160

4261
def isPrime(number):
4362
"""
@@ -602,4 +621,4 @@ def fib(n):
602621
ans += fib1
603622
fib1 = tmp
604623

605-
return ans
624+
return ans

0 commit comments

Comments
 (0)