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

Thank you Gribouillis!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.