Skip to content

Instantly share code, notes, and snippets.

@apatrascu
Created June 1, 2017 13:01
Show Gist options
  • Save apatrascu/5fe51c9c6ebad2adbfb1cf06105fdc0f to your computer and use it in GitHub Desktop.
Save apatrascu/5fe51c9c6ebad2adbfb1cf06105fdc0f to your computer and use it in GitHub Desktop.
from memory_profiler import profile
@profile(precision=6)
def primes(n):
if n == 2:
return [2]
elif n < 2:
return []
s = range(3, n + 1, 2)
mroot = n ** 0.5
half = (n + 1) / 2 - 1
i = 0
m = 3
while m <= mroot:
if s[i]:
j = (m * m - 3) / 2
s[j] = 0
while j < half:
s[j] = 0
j += m
i = i + 1
m = 2 * i + 3
l = [2]
for x in s:
if x:
l.append(x)
return l
len(primes(100000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment