I am new to Python and was asked to create a program that would take an input as a non-negative integer n and then compute an approximation for the value of e using the first n + 1 terms of the continued fraction:
I have attempted to decipher the question but can't exactly understand everything it is asking. I am not looking for an exact answer but hopefully an example to help me on my way.
This is the exact question
Below is a code I have done with continued fractions before.
import math
# Get x from user
x = float(input("Enter x = "))
# Calculate initial variables and print
a0 = x//1
r0 = x-a0
print("a0 =", a0, "\tr0 =", r0)
# Calculate ai and ri for i = 1,2,3 and print results
a1 = 1/r0//1
r1 = 1/r0 - a1
print("a1 =", a1, "\tr1 =", r1)
a2 = 1/r1//1
r2 = 1/r1 - a2
print("a2 =", a2, "\tr2 =", r2)
a3 = 1/r2//1
r3 = 1/r2 - a3
print("a3 =", a3, "\tr3 =", r3)
The value e can be expressed as the limit of the following continued fraction:
The initial
2 + 1 /
falls outside of the main pattern, but after that it just continues as shown. Your job is to evaluate this up ton
deep, at which point you stop and return the value up to that point.Make sure you carry out the calculation in floating point.