How can I speed up a script that iterates over a large range (600 billion)?
Paul Rubin
no.email at nospam.invalid
Wed Jun 22 02:23:12 EDT 2011
Chris Torek <nospam at 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))
More information about the Python-list
mailing list