Skip to content

Instantly share code, notes, and snippets.

@adituv
Last active December 20, 2015 03:49
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 adituv/6066399 to your computer and use it in GitHub Desktop.
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.
#!/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