Prime number algo... what's wrong?

Anton Vredegoor anton at
Sat Mar 22 16:21:47 CET 2003

"L. B." <lorenzo at> wrote:

>> Hmmm... so if test is not not 0 and it's not 0, what else can it
>> be? :-)
>I'll work with the neurons i have left on it! ;-)

Please check if it's in the list of numbers this generator produces.
(The test breaks after x > 100, but that's not provably correct


from __future__ import generators
from math import sqrt

_pp = [2,3]

def primes():
    #prime number generator
    for p in _pp:
        yield p
    i = p
    while 1:
        i += 2
        cutoff = int(sqrt(i))
        for p in _pp:
            if i % p == 0 or p > cutoff:
        if i % p <> 0:
            yield i

def test():
    for x in primes():
        if x > 100:
        print x

if __name__=='__main__':

More information about the Python-list mailing list