PyEuler

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Mon Feb 25 23:25:34 CET 2008


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.


> There is a full set of solutions on the haskell wiki, if anyone cares.

Thank you for the suggestion, I have found it:
http://www.haskell.org/haskellwiki/Euler_problems
In the last months lot of people are suddenly talking about Haskell
(and sometimes even F#!). I think the Haskell language is too much
complex for me for bigger programs, but for such small mathematical
programs it looks very nice and almost readable.

This is a curious comment from that Wiki about a solution to the
Problem 2:
>The first two solutions work because 10^6 is small. The following solution also works for much larger numbers (up to at least 10^1000000 on my computer):<

What I like of the Haskell community is how much they keep things tidy
and ordered. And every function used on the Wiki points to the
documentation! They create a spotless wiki about Shootout benchmarks,
Euler problems, and so on, they discuss al lot and they try to find
fast & correct solutions. They even *improve* their language when they
find some benchmarks of the Shootout run too much slow with Haskell,
because they have understood that despite being most little (silly)
programs, the benchmarks of the Shootout site are rather well thought
out. Both the Python and D communities that I know (and I appreciate)
aren't like that. I presume the Haskell language itself appeals to
people that like tidy and well ordered things, and when you use that
language a lot it may encourage your mind to think even more in that
way. Computer languages too make the man :-)

Bye,
bearophile



More information about the Python-list mailing list