Skip to content

Instantly share code, notes, and snippets.

@telliott99
Last active September 30, 2018 21:07
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/19f521c807210171a4847b319104b3df to your computer and use it in GitHub Desktop.
Save telliott99/19f521c807210171a4847b319104b3df to your computer and use it in GitHub Desktop.
Archimedes method to compute pi
p = 4.0/(2**0.5)
P=4
def one_round(t):
p,P = t
P2 = 2*p*P/(p+P)
p2 = (p*P2)**0.5
return p2,P2
s = '%3.10f %3.10f'
print '%2d' % 2, s % (p,P)
for i in range(3,20):
p,P = one_round((p,P))
print '%2d' % i, s % (p,P)
```
> python pi.py
2 2.8284271247 4.0000000000
3 3.0614674589 3.3137084990
4 3.1214451523 3.1825978781
5 3.1365484905 3.1517249074
6 3.1403311570 3.1441183852
7 3.1412772509 3.1422236299
8 3.1415138011 3.1417503692
9 3.1415729404 3.1416320807
10 3.1415877253 3.1416025103
11 3.1415914215 3.1415951177
12 3.1415923456 3.1415932696
13 3.1415925766 3.1415928076
14 3.1415926343 3.1415926921
15 3.1415926488 3.1415926632
16 3.1415926524 3.1415926560
17 3.1415926533 3.1415926542
18 3.1415926535 3.1415926537
19 3.1415926536 3.1415926536
>
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment