This program is to produce prime numbers less than and equal to n.
1 2 3 4 5 6 7 8 9 10 11 12 | def primes(n):
if n <= 1:
print('No primes.')
return False
n = int(n)
p = list(range(1,n+1,2)) # in Python 3, range can't be assigned;
q = len(p)
p[0] = 2
for i in range(3,int(n**.5)+1,2):
if p[(i-1)//2]:
p[(i*i-1)//2:q:i] = [0]*((q-(i*i+1)//2)//i+1)
return [x for x in p if x]
|
As I tested so far, this is the fastest python code.