Working with the set of real numbers (was: Finding size of Variable)
Ian Kelly
ian.g.kelly at gmail.com
Tue Mar 4 12:23:04 CET 2014
On Tue, Mar 4, 2014 at 4:19 AM, Ian Kelly <ian.g.kelly at 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
More information about the Python-list
mailing list