Guest User

Untitled

a guest
Aug 15th, 2016
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.74 KB | None | 0 0
  1. import argparse
  2.  
  3. def main(args):
  4.     n = 0
  5.     d = 1
  6.     p = 2
  7.    
  8.     for i in range(args.iterations):
  9.         if i%2:
  10.             d += 2
  11.         else:
  12.             n += 2
  13.            
  14.         p *= (n/d)
  15.  
  16.         if args.verbose:
  17.             print("%d: %f" % (i+1,p))
  18.    
  19.     print(p)
  20.  
  21. if __name__ == '__main__':
  22.     parser = argparse.ArgumentParser(description='Calculate pi using the Wallis product formula.')
  23.     parser.add_argument('iterations', nargs='?', default=1000000, type=int,  help='Number of terms in the product')
  24.     parser.add_argument('-v', '--verbose', dest='verbose', action='store_true',  help='Show the result of each iteration (warning: huge performance penalty)')
  25.     args = parser.parse_args()
  26.     main(args)
Advertisement
Add Comment
Please, Sign In to add comment