Skip to content

Added a calculate PI function #309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 16, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion primelib/primelib.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@
goldbach(number) // Goldbach's assumption

"""
def pi(maxK=70, prec=1008, disp=1007):
"""
maxK: nuber of iterations
prec: precision of decimal places
disp: number of decimal places shown
"""
from decimal import Decimal as Dec, getcontext as gc
gc().prec = prec
K, M, L, X, S = 6, 1, 13591409, 1, 13591409
for k in range(1, maxK+1):
M = Dec((K**3 - (K<<4)) * M / k**3)
L += 545140134
X *= -262537412640768000
S += Dec(M * L) / X
K += 12
pi = 426880 * Dec(10005).sqrt() / S
pi = Dec(str(pi)[:disp])
return pi


def isPrime(number):
"""
Expand Down Expand Up @@ -602,4 +621,4 @@ def fib(n):
ans += fib1
fib1 = tmp

return ans
return ans
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After I created this pull request I noticed this. This change was accidental.