Created
February 22, 2016 18:02
-
-
Save sergiogarciadev/5d35a85f07d0a7b60c67 to your computer and use it in GitHub Desktop.
Calculate pi up to n digits
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| def make_pi(digits): | |
| def _make_pi(digits): | |
| q, r, t, k, m, x = 1, 0, 1, 1, 3, 3 | |
| count = -1 | |
| for j in range(sys.maxsize): | |
| if count >= digits: | |
| break | |
| if 4 * q + r - t < m * t: | |
| count += 1 | |
| yield m | |
| q, r, t, k, m, x = 10*q, 10*(r-m*t), t, k, (10*(3*q+r))//t - 10*m, x | |
| else: | |
| q, r, t, k, m, x = q*k, (2*q+r)*x, t*x, k+1, (q*(7*k+2)+r*x)//(t*x), x+2 | |
| n=[str(i) for i in _make_pi(digits)] | |
| n = n[:1] + ['.'] + n[1:] | |
| return ''.join(n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment