The code below takes integer n as input, and delivers a list of all primes up to integer n using the Sieve of Eratosthenes.
My question is, could you please help me optimize this code? Is it considered poor code to use 'try' flow control? I'd like to optimize the code such that I don't need it.
def factorfinder(n):
A = range(0,n+1)
for i in xrange(0,int(math.sqrt(n))):
if i == 0 or i == 1:
A[i] = 0
continue
for j in xrange(0,n):
try:
A[i**2+j*i] = 0
except IndexError:
pass
return filter(lambda x: x != 0, A)
i = 6k-1
ori = 6k+1
. See: primes.utm.edu/notes/faq/six.html So, you only need test 5,7,11,13,17,19,23,25,29,31,... \$\endgroup\$int(math.sqrt(n))+1
. \$\endgroup\$