Prime number algo... what's wrong?
Anton Vredegoor
anton at vredegoor.doge.nl
Sat Mar 22 16:21:47 CET 2003
"L. B." <lorenzo at mysurname.net> 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
programming)
Anton.
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:
break
if i % p <> 0:
_pp.append(i)
yield i
def test():
for x in primes():
if x > 100:
break
print x
if __name__=='__main__':
test()
More information about the Python-list
mailing list