On Tue, Mar 4, 2014 at 4:19 AM, Ian Kelly <ian.g.ke...@gmail.com> wrote: > def cf_sqrt(n): > """Yield the terms of the square root of n as a continued fraction.""" > m = 0 > d = 1 > a = a0 = floor_sqrt(n) > while True: > yield a > next_m = d * a - m > next_d = (n - next_m * next_m) // d > if next_d == 0: > break > next_a = (a0 + next_m) // next_d > m, d, a = next_m, next_d, next_a
Sorry, all that "next" business is totally unnecessary. More simply: def cf_sqrt(n): """Yield the terms of the square root of n as a continued fraction.""" m = 0 d = 1 a = a0 = floor_sqrt(n) while True: yield a m = d * a - m d = (n - m * m) // d if d == 0: break a = (a0 + m) // d -- https://mail.python.org/mailman/listinfo/python-list