Skip to content

Instantly share code, notes, and snippets.

@rafaelmv
Created August 24, 2017 14:56
Show Gist options
  • Save rafaelmv/c4c9a3ca51e72b55fb6c7a9fb021f31e to your computer and use it in GitHub Desktop.
Save rafaelmv/c4c9a3ca51e72b55fb6c7a9fb021f31e to your computer and use it in GitHub Desktop.
from __future__ import print_function, division
import sys
def contfrac_to_frac(seq):
''' Convert the simple continued fraction in `seq`
into a fraction, num / den
'''
num, den = 1, 0
for u in reversed(seq):
num, den = den + num*u, num
return num, den
def e_cont_frac(n):
''' Build `n` terms of the simple continued fraction expansion of e
`n` must be a positive integer
'''
seq = [2 * (i+1) // 3 if i%3 == 2 else 1 for i in range(n)]
seq[0] += 1
return seq
def main():
# Get the the number of terms, less one
n = int(sys.argv[1]) if len(sys.argv) > 1 else 11
if n < 0:
print('Argument must be >= 0')
exit()
n += 1
seq = e_cont_frac(n)
num, den = contfrac_to_frac(seq)
print('Terms =', n)
print('Continued fraction:', seq)
print('Fraction: {0} / {1}'.format(num, den))
print('Float {0:0.15f}'.format(num / den))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment