Advertisement
MGakowski

Pi Calc Nilakantha series.py

Jan 25th, 2017
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.11 KB | None | 0 0
  1. from decimal import *
  2.  
  3. print '[#] The Nilakantha "infinite" series for calculating Pi!'
  4. print "[#] While somewhat more complicated, it converges on pi much quicker than the Leibniz formula. " \
  5.       "Carry this out even a few times and the results get fairly close to pi."
  6.  
  7. pi = Decimal(3)
  8. loop = int(0)
  9. iterats = int(raw_input("Iterations? "))
  10. inc = int(raw_input("Increments? "))
  11. inccheck = int(loop+inc)
  12. getcontext().prec = int(raw_input("Number of decimal places? "))+1
  13.  
  14. a = int(2)
  15. b = int(3)
  16. c = int(4)
  17.  
  18.  
  19. while loop < iterats:
  20.     if loop == inccheck:
  21.         print str("*Pi is " + str(pi) + " after " + str(loop) + " iterations.")
  22.         inccheck += inc
  23.  
  24.     abc = Decimal(a * b * c)
  25.  
  26.     pi += 4 / Decimal(abc)
  27.     a += 2
  28.     b += 2
  29.     c += 2
  30.     loop += 1
  31.  
  32.     if loop == inccheck:
  33.         print str("*Pi is " + str(pi) + " after " + str(loop) + " iterations.")
  34.         inccheck += inc
  35.  
  36.     abc = Decimal(a * b * c)
  37.  
  38.     pi -= 4 / Decimal(abc)
  39.     a += 2
  40.     b += 2
  41.     c += 2
  42.     loop += 1
  43.  
  44. print str("LIMIT REACHED! Pi is " + str(pi) + " after " + str(loop) + " iterations.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement