Skip to content

Instantly share code, notes, and snippets.

@SeijiEmery
Last active August 6, 2017 17:42
Show Gist options
  • Select an option

  • Save SeijiEmery/c6139d82ab7cbce8eeaef02e89358475 to your computer and use it in GitHub Desktop.

Select an option

Save SeijiEmery/c6139d82ab7cbce8eeaef02e89358475 to your computer and use it in GitHub Desktop.
pi in a nutshell
#!/usr/bin/env python
import math
from math import sqrt
def pi (n):
x = 1
for i in range(n):
x = 2 - sqrt(4 - x)
return 3 * 2 ** n * sqrt(x)
if __name__ == '__main__':
fmt = lambda xs: '\n'.join([ '%2d: %20.20f %20.20f'%x for x in xs ])
print('%2s: %20.20s %20.20s'%('n', 'pi(n)', 'math.pi - pi(n)'))
print(fmt([ (n, pi(n), math.pi - pi(n)) for n in range(30) ]))
n: pi(n) math.pi - pi(n)
0: 3.00000000000000000000 0.14159265358979311600
1: 3.10582854123024976190 0.03576411235954335410
2: 3.13262861328123687343 0.00896404030855624256
3: 3.13935020304687206760 0.00224245054292104840
4: 3.14103195089052977806 0.00056070269926333793
5: 3.14145247228534429951 0.00014018130444881649
6: 3.14155760791162208534 0.00003504567817103066
7: 3.14158389214893585262 0.00000876144085726338
8: 3.14159046323676172108 0.00000219035303139492
9: 3.14159210604304828252 0.00000054754674483348
10: 3.14159251658815463770 0.00000013700163847830
11: 3.14159261864078942494 0.00000003494900369105
12: 3.14159264532121573765 0.00000000826857737835
13: 3.14159264532121573765 0.00000000826857737835
14: 3.14159264532121573765 0.00000000826857737835
15: 3.14159264532121573765 0.00000000826857737835
16: 3.14159230381173770752 0.00000034977805540848
17: 3.14159230381173770752 0.00000034977805540848
18: 3.14158683965504126334 0.00000581393475185266
19: 3.14158683965504126334 0.00000581393475185266
20: 3.14167426502175750613 -0.00008161143196439014
21: 3.14167426502175750613 -0.00008161143196439014
22: 3.14307274017003956956 -0.00148008658024645356
23: 3.13747509950278313795 0.00411755408700997805
24: 3.18198051533946379976 -0.04038786174967068376
25: 3.35410196624968470758 -0.21250931265989159158
26: 3.00000000000000000000 0.14159265358979311600
27: 0.00000000000000000000 3.14159265358979311600
28: 0.00000000000000000000 3.14159265358979311600
29: 0.00000000000000000000 3.14159265358979311600
[Finished in 0.2s]
@SeijiEmery

SeijiEmery commented Feb 20, 2017

Copy link
Copy Markdown
Author

This is an elegant algorithm for calculating PI that uses only addition/subtraction and one sqrt operation per iteration.
– Derived from Archimedes' method using inscribed regular hexagons: https://i.imgur.com/ijqXUas.jpg
– Precision is limited by the sqrt + numerics implementation (to go beyond n = 15 requires a bignum implementation w/ an extremely accurate sqrt function (which in turn requires an accurate division / reciprocal function, etc). Division + sqrt() are both iterative algorithms (with pi that's 3 levels of iteration), so this seemingly simple algorithm can be extremely expensive to calculate at very high levels of precision. This isn't a terribly efficient way to calculate pi, but it's wonderfully simple, isn't it? :) ).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment