Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import argparse
- def main(args):
- n = 0
- d = 1
- p = 2
- for i in range(args.iterations):
- if i%2:
- d += 2
- else:
- n += 2
- p *= (n/d)
- if args.verbose:
- print("%d: %f" % (i+1,p))
- print(p)
- if __name__ == '__main__':
- parser = argparse.ArgumentParser(description='Calculate pi using the Wallis product formula.')
- parser.add_argument('iterations', nargs='?', default=1000000, type=int, help='Number of terms in the product')
- parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', help='Show the result of each iteration (warning: huge performance penalty)')
- args = parser.parse_args()
- main(args)
Advertisement
Add Comment
Please, Sign In to add comment