title: The Chudnovsky Formula in Python
body: <p>I'm trying to implement the Chudnovsky algorithm for calculating pi.
  I am using formulas from this description <a href="https://www.craig-wood.com/nick/articles/pi-chudnovsky/" rel="nofollow noreferrer">https://www.craig-wood.com/nick/articles/pi-chudnovsky/</a></p>
  
  <p>Now it's working,  but maximum number of digits it can show is
  3.141592653589793238462643385 - only <strong>27</strong> digits.</p>
  
  <p>Why does Python limits the number of digits in this script?
  May be i am using <strong>Decimal</strong> in wrong way?</p>
  
  <p>Here is my code (updated):</p>
  
  <pre><code>from decimal import Decimal, getcontext
  from math import factorial
  import sys
  
  def calculate_pi(max_K, number_of_digits):
      getcontext.prec = number_of_digits+2
      a_k, b_k, C, a_sum, b_sum  =  1, 0, 640320, 1, 0
      for k in range(1,max_K):
          a_k *= -(Decimal(24)/Decimal(C**3))*Decimal((6*k-5)*(2*k-1)*(6*k-1))/Decimal(k**3)
          a_sum += a_k
          b_sum += a_k*k
  
  
      pi = 426880*Decimal(10005).sqrt()/Decimal(13591409*a_sum + 545140134*b_sum)
      print str(pi)[:number_of_digits+2]
  
  def main(number_of_digits):
      pi = calculate_pi(10000, number_of_digits)
  
  
  if __name__ == "__main__":
      number_of_digits = int(sys.argv[1])
      main(number_of_digits)
  </code></pre>