SimplePrograms challenge

Steven Bethard steven.bethard at gmail.com
Thu Jun 14 14:22:09 EDT 2007


rzed wrote:
> Steven Bethard <steven.bethard at gmail.com> wrote in
>> def iter_primes():
>>      # an iterator of all numbers between 2 and +infinity
>>      numbers = itertools.count(2)
>>
>>      # generate primes forever
>>      while True
>>
>>          # generate the first number from the iterator,
>>          # which should always be a prime
>>          prime = numbers.next()
>>          yield prime
>>
>>          # lazily remove all numbers from the iterator that
>>          # are divisible by prime we just selected
>>          numbers = itertools.ifilter(prime.__rmod__, numbers)
>>
>> I think that's 17-ish, though you could shrink it down by
>> removing some of the spaces.
> 
> How about including a driver?

Yes, absolutely a good idea. Fortunately, the other Steve borrowed the 
time machine already and added this to the end::

     for p in iter_primes():
         if p > 1000: break
         print p

http://wiki.python.org/moin/SimplePrograms

STeVe



More information about the Python-list mailing list