Skip to content

Instantly share code, notes, and snippets.

@corehello
Last active March 6, 2025 17:26
Show Gist options
  • Save corehello/20eae6b80c4b18600df9125daf9861c9 to your computer and use it in GitHub Desktop.
Save corehello/20eae6b80c4b18600df9125daf9861c9 to your computer and use it in GitHub Desktop.
continued fraction of e
# %%
# continued fraction of e
# https://mathworld.wolfram.com/eContinuedFraction.html
# e = 1+1+1/(1+1/(2+2/(3+3/(4+4/(5+5/(...))))))
def eContinuedFraction(n: int) -> float:
ret = 1
for i in range(n, 0, -1):
ret = i+i/ret
return 1+1+1/ret
# %%
# continued fraction of pi
https://mathworld.wolfram.com/PiContinuedFraction.html
def piContinuedFraction(n: int):
ret = 1
for i in range(n, 0, -1):
ret = 1-(1 if i==1 else (2*i-3)*(2*i-2))/(3-(2*i*(2*i+1))/ret)
return 2*ret
# %%
# continued fraction of pi
https://mathworld.wolfram.com/PiContinuedFraction.html
def piContinuedFraction(n: int):
ret = 1
for i in range(n, -1, -1):
ret = 2+((2*i+1)**2)/ret
return 4/(ret - 1)
@corehello
Copy link
Author

corehello commented Mar 6, 2025

print(eContinuedFraction(20))
2.7182818284590455
piContinuedFraction(100000)
3.1415826536897913

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment