Skip to content

Instantly share code, notes, and snippets.

@uiur
Created June 3, 2016 02:01
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 uiur/f912f2c8cac870cf654489da568cb68f to your computer and use it in GitHub Desktop.
Save uiur/f912f2c8cac870cf654489da568cb68f to your computer and use it in GitHub Desktop.
def pi(n):
half_pi = 0
for k in range(n + 1):
m = 1.0
for i in range(1, k + 1):
m *= float(2 * i - 1) / (2 * i)
half_pi += m / (2 * k + 1)
return 2 * half_pi
for i in range(1000):
print("%d: %f" % (i, pi(i)))
@uiur
Copy link
Author

uiur commented Jun 3, 2016

0: 2.000000
1: 2.333333
2: 2.483333
3: 2.572619
4: 2.633383
5: 2.678127
6: 2.712833
7: 2.740762
8: 2.763866
9: 2.783389
...
980: 3.105565
981: 3.105583
982: 3.105601
983: 3.105620
984: 3.105638
985: 3.105656
986: 3.105674
987: 3.105693
988: 3.105711
989: 3.105729
990: 3.105747
991: 3.105765
992: 3.105783
993: 3.105801
994: 3.105819
995: 3.105837
996: 3.105855
997: 3.105873
998: 3.105891
999: 3.105909

@uiur
Copy link
Author

uiur commented Jun 3, 2016

❯ python pi.py
10000: 3.130309

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