[Tutor] primes - sieve of odds

Sean Perry shaleh at speakeasy.net
Sat Mar 26 17:47:49 CET 2005


John Miller wrote:
> How does one actually use this module? For example:
> 
>  >>> import eratosthenes
>  >>> eratosthenes.primes()
> <generator object at 0x640d0>
>  >>> eratosthenes.primes().next()
> 2
>  >>> eratosthenes.primes().next()
> 2
>  >>> eratosthenes.primes().next()
> 2
> 
> How does one get beyond the first prime?
> 

it = eratosthenes.primes()
it.next()
it.next()

or

# 'it' is shorthand for iterator
def primesToN(n):
     it = eratosthenes.primes()
     return [ x for x in it if x <= n ]

You were running into problems because the primes() function returns a 
new generator. The values are in the generator not the function primes().


More information about the Tutor mailing list