0

The following code iteratively calculates continued fractions. I'm having trouble separating the numerator and denominator from the returned value.

def cf3(terms, iterations):
    
    answer = 0
    
    for n in range(iterations, 0, -1):
        answer = Fraction(1, terms[n] + answer)
        
    answer += Fraction(terms[0], 1)

    return answer

The following is sample output showing the first four approximations of sqrt 7.

7 [2, 1, 1, 1, 4]
1 3
2 5/2
3 8/3
4 37/14

Thank you from a new poster

2
Contributors
2
Replies
4
Views
7 Years
Discussion Span
Last Post by debragail
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.