Skip to content

Instantly share code, notes, and snippets.

@telliott99
Created April 17, 2021 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save telliott99/e9ff4bb597885d4ef5b7116cb0b38ad7 to your computer and use it in GitHub Desktop.
Save telliott99/e9ff4bb597885d4ef5b7116cb0b38ad7 to your computer and use it in GitHub Desktop.
import sys, math
try:
r = int(sys.argv[1])
R = range(r)
except:
print('enter the number of cycles')
sys.exit()
def f(sin,cos):
cot = cos/sin
csc = 1/sin
while True:
yield 1/csc
cot = cot + csc
csc = math.sqrt(1 + cot**2)
sq = math.sqrt(3)
g = f(0.5,sq/2)
n = 6
for i in R:
print('%2d: %3.14f' % (i+1, n*next(g)))
n *= 2
print('pi: ', math.pi)
'''
> p3 archimedes.py 24
1: 3.00000000000000
2: 3.10582854123025
3: 3.13262861328124
4: 3.13935020304687
5: 3.14103195089051
6: 3.14145247228546
7: 3.14155760791186
8: 3.14158389214832
9: 3.14159046322805
10: 3.14159210599927
11: 3.14159251669216
12: 3.14159261936538
13: 3.14159264503369
14: 3.14159265145077
15: 3.14159265305504
16: 3.14159265345610
17: 3.14159265355637
18: 3.14159265358144
19: 3.14159265358770
20: 3.14159265358927
21: 3.14159265358966
22: 3.14159265358976
23: 3.14159265358979
24: 3.14159265358979
pi: 3.141592653589793
>
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment