Prime number algo... what's wrong?
L. B.
lorenzo at mysurname.net
Sat Mar 22 08:08:37 EST 2003
Hi all,
it's two days i'm trying to make this stuff work!! ;-) I really may be
dumb! Now i think i'm on the right way and close to the result but
still the algo doesn't work... could you please tell me what i'm
missing?
Also are you aware of more sophisticated and efficient algos for prime
testing implemented in Python?
Thank You,
Lorenzo
Code follows...
--begin--
import math
mayP = 1 #num to test
pCount = 0 #prime counter
while mayP < 100: #find primes < 100
x = 2 #num to divide for
eLoop = round(math.sqrt(mayP) + 1.5) #eLoop = end Loop
while x < eLoop:
test = (mayP % x) #mayP mod x
if test != 0:
x += 1 #still may be prime so go on trying
elif test == 0:
mayP += 1 #is not prime, test the second prime candidate
else:
pCount += 1 #increment counter
isP = mayP #may --> is
print "%u | %u" % (pCount, isP) #uhm... prints results?!
;)
mayP += 1
--eof--
More information about the Python-list
mailing list