PyEuler

Arnaud Delobelle arnodel at googlemail.com
Mon Feb 25 17:47:20 EST 2008


On Feb 25, 10:25 pm, bearophileH... at lycos.com wrote:
> Paul Rubin:
>
> >     def ggenfib():
> >       a,b = 1,2
> >       while True:
> >          yield a
> >          a,b = b, a=b
>
> Your version is the nice basic generator version (the last line
> contains a +, I presume), but I like this other version too:
>
> def xfibonacci():
>     a = b = 1
>     yield a
>     yield b
>     while True:
>         a = a + b
>         yield a
>         b = b + a
>         yield b
>
> It's a bit faster, longer, and I find it a bit more readable.

In this case why not:

def xfib(a=1, b=1):
    while True:
        yield a
        a += b
        yield b
        b += a

--
Arnaud




More information about the Python-list mailing list