Advertisement
MagicWinnie

Untitled

Mar 15th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | None | 0 0
  1. from decimal import Decimal as Dec, getcontext as gc
  2.  
  3. def PI(maxK=70, prec=1008, disp=1007): # parameter defaults chosen to gain 1000+ digits within a few seconds
  4.     gc().prec = prec
  5.     K, M, L, X, S = 6, 1, 13591409, 1, 13591409
  6.     for k in range(1, maxK+1):
  7.         M = (K**3 - 16*K) * M // k**3
  8.         L += 545140134
  9.         X *= -262537412640768000
  10.         S += Dec(M * L) / X
  11.         K += 12
  12.     pi = 426880 * Dec(10005).sqrt() / S
  13.     pi = Dec(str(pi)[:disp]) # drop few digits of precision for accuracy
  14.     print("PI(maxK=%d iterations, gc().prec=%d, disp=%d digits) =\n%s" % (maxK, prec, disp, pi))
  15.     return pi
  16.  
  17. Pi = PI(353,5022,5020)
  18. print("\nFor greater precision and more digits (takes a few extra seconds) - Try")
  19. print("Pi = PI(317,4501,4500)")
  20. print("Pi = PI(353,5022,5020)")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement