Created
May 22, 2012 01:45
-
-
Save pylemon/2766035 to your computer and use it in GitHub Desktop.
python: calculate the value of PI(pi)
This file contains 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
#!/usr/bin/env python | |
def f((q, r, t, k)): | |
n = (3 * q + r) / t | |
if (4 * q + r) / t == n: | |
return (10 * q, 10 * (r - n * t), t, k, n) | |
else: | |
return (q * k, q * (4 * k + 2) + r * (2 * k + 1), t * (2 * k + 1), k + 1) | |
def pi(n = -1): | |
out = '' | |
printed = False | |
r = f((1, 0, 1, 1)) | |
out = '' | |
while (n != 0): | |
if len(r) == 5: | |
out += str(r[4]) | |
if not printed: | |
out += '.' | |
printed = True | |
n -= 1 | |
r = f(r[:4]) | |
return out | |
def main(): | |
print pi(10000) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment