Advertisement
AlfonsoPEREZ

PI IN PYTHON AGAIN

Apr 27th, 2020
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.78 KB | None | 0 0
  1. from decimal import Decimal, getcontext
  2. from math import factorial
  3. import sys
  4.  
  5. def calculate_pi(max_K, number_of_digits):
  6.     getcontext.prec = number_of_digits+2
  7.     a_k, b_k, C, a_sum, b_sum  =  1, 0, 640320, 1, 0
  8.     for k in range(1,max_K):
  9.         a_k *= -(Decimal(24)/Decimal(C**3))*Decimal((6*k-5)*(2*k-1)*(6*k-1))/Decimal(k**3)
  10.         a_sum += a_k
  11.         b_sum += a_k*k
  12.  
  13.  
  14.     pi = 426880*Decimal(10005).sqrt()/Decimal(13591409*a_sum + 545140134*b_sum)
  15.     print str(pi)[:number_of_digits+2]
  16.  
  17. def main(number_of_digits):
  18.     pi = calculate_pi(10000, number_of_digits)
  19.  
  20.  
  21. if __name__ == "__main__":
  22.     number_of_digits = int(sys.argv[1])
  23.     main(number_of_digits)
  24.  
  25. #MADE BY AVMP
  26. #PLEASE CHECK OUT MY YT CHANNEL https://www.youtube.com/channel/UCQor7IURWM-lGT-tmFbFSCw
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement