Created
May 8, 2012 21:57
-
-
Save killerswan/2639705 to your computer and use it in GitHub Desktop.
pi
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
| # Ramanujan's PI algorithm, per Think Python | |
| # | |
| # >>> execfile("C:/[snip]/Desktop/ramanujan.py") | |
| # >>> rpi() | |
| import math | |
| def rpi (): | |
| k = 0 | |
| estimate = 0.0 | |
| start = True | |
| while start == True or term >= 1e-15: | |
| start = False | |
| term = innerTerm(k) * 2 * math.sqrt(2) / 9801 | |
| estimate += term | |
| k += 1 | |
| return 1/estimate | |
| def innerTerm(k): | |
| a = math.factorial(4*k) * (1103+26390*k) | |
| b = (math.factorial(k)**4) * (396**(4*k)) | |
| return a / b |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment