Chris Torek <nos...@torek.net> writes:
> def primes():
>     """
>     Yields sequence of prime numbers via Sieve of Eratosthenes.
>     """

I think this has the same order-complexity but is enormously slower in
practice, and runs out of recursion stack after a while.  Exercise: spot
the recursion.

    from itertools import islice, ifilter, count

    def primes():
        a = count(2)
        while True:
            p = a.next()
            yield p
            a = ifilter(lambda t,p=p: t%p, a)

    # print first 100 primes
    print list(islice(primes(), 100))
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to