PyEuler

Paul Rubin http
Mon Feb 25 14:25:34 EST 2008


bearophileHUGS at lycos.com writes:

> def takenth(n, iterable):
>     "Returns the nth item"
>     return list(islice(iterable, n, n+1))[0]
> 

return islice(iterable, n).next()

>     isanymultiple = lambda x: any((x % y == 0) for y in nums)
>     return sum(filter(isanymultiple, xrange(end)))

This isn't so good, you really want to apply the filters recursively.

> def fibonacci(n):
>     """Return nth element of the fibonacci serie"""
>     if n == 0 or n == 1:
>         return n
>     return fibonacci(n-1) + fibonacci(n-2)

uggh!!!! exponential blowup!

> def euler2(end):
>     genfib = imap(fibonacci, count())

Are you kidding?
    def ggenfib():
      a,b = 1,2
      while True: 
         yield a
         a,b = b, a=b

> What I think about such code:
> - It's not much readable (but usually it can be read). ...

Your observations are generally good; I'd say it was done
without enough attention to the math too.

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



More information about the Python-list mailing list