Recently interested in simple primality tests. I have the following two functions that return a list of all primes up to the given input. The first I made, the other is based off wikipedia's pseudocode for primality tests. I then slightly altered mine to be what I thought was the closest to wikipedia's.
When I time them (with 10000 as the input/limit), mine takes an order of magnitude longer than the other. I'm not quite sure why, as to me they're doing really similar things. Mine checks through a list of primes with "any", whereas wiki's checks through those same numbers but generates them with a while loop. What am I missing?
def my_primality(lim):
count = 5
slbprimes = [2,3];
while count<lim:
if count%3==0:
pass
elif any(count%i==0 for i in slbprimes if i**2<=count):
pass
else:
slbprimes.append(count)
count+=2
return slbprimes
def evenbetter(lim):
count = 5
ebprimes=[2,3];
while count < lim:
if count%3==0:
count+=2
continue
i=5
while i**2<=count:
if count%i==0 or count%(i+2)==0:
count+=2
break
i=i+6
else:
ebprimes.append(count)
count+=2
return ebprimes