MGakowski

Pi Calc Gregory-Leibniz series.py

Jan 25th, 2017
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.96 KB | None | 0 0
  1. from decimal import *
  2.  
  3. print '[#] The Gregory-Leibniz "infinite" series for calculating Pi!'
  4. print "[#] Though not very efficient, it will get closer and closer to pi with every iteration, accurately producing " \
  5.       "pi to five decimal places with 500,000 iterations."
  6.  
  7. pi = Decimal(0)
  8. odd = Decimal(1)
  9. loop = int(0)
  10. iterats = int(raw_input("Iterations? "))
  11. inc = int(raw_input("Increments? "))
  12. inccheck = int(loop+inc)
  13. getcontext().prec = int(raw_input("Number of decimal places? "))+1
  14.  
  15.  
  16. while loop < iterats:
  17.  
  18.     if loop == inccheck:
  19.         print str("*Pi is " + str(pi) + " after " + str(loop) + " iterations.")
  20.         inccheck += inc
  21.  
  22.     pi += 4 / odd
  23.     odd += 2
  24.     loop += 1
  25.  
  26.     if loop == inccheck:
  27.         print str("*Pi is " + str(pi) + " after " + str(loop) + " iterations.")
  28.         inccheck += inc
  29.  
  30.     pi -= 4 / odd
  31.     odd += 2
  32.     loop += 1
  33.  
  34. print str("LIMIT REACHED! Pi is " + str(pi) + " after " + str(loop) + " iterations.")
Advertisement
Add Comment
Please, Sign In to add comment