Skip to content

Instantly share code, notes, and snippets.

@dbb
Created July 27, 2011 19:44
Show Gist options
  • Save dbb/1110203 to your computer and use it in GitHub Desktop.
Save dbb/1110203 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
if len(sys.argv) < 2:
sys.exit('Usage: %s [INTEGER]' % sys.argv[0])
try:
n = int( sys.argv[1] )
except ValueError:
sys.exit('Usage: %s [INTEGER]' % sys.argv[0])
sum = 0
for i in range(n):
delta = 4.0 / (2*i +1)
if i % 2 == 0:
sum += delta
elif i % 2 == 1:
sum -= delta
else:
print('only non-negitive integers')
print( "\nLeibniz approximation for π after " + str(n) + " iterations:\n\n"
+ str(sum) + "\n" )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment