Last active
December 20, 2015 03:49
-
-
Save adituv/6066399 to your computer and use it in GitHub Desktop.
A demonstration of Khinchin's constant for the continued fraction expansion of the number pi. Obviously as python has finite precision floating point, this is only an approximation, and is likely to approach an incorrect result as a grows larger.
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/python3 | |
from math import floor | |
from math import pi | |
def getExpansion(y): | |
x = y | |
while True: | |
z = floor(x) | |
yield z | |
z = 1/(x-z) | |
def main(count=2000): | |
x = pi | |
r = getExpansion(x) | |
acc = 1.0 | |
print("a_n\tgeo. mean") | |
for a in range(1,count+1): | |
rn = next(r) | |
acc *= pow(rn,1/a) | |
print("{}\t{}".format(rn,acc)) | |
acc = pow(acc,a/(a+1)) | |
if __name__ == "__main__": | |
if len(argv) > 1: | |
main(int(argv[1])) | |
else: | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment