Skip to content

Instantly share code, notes, and snippets.

@meehow
Created March 4, 2011 14:20
Show Gist options
  • Save meehow/854669 to your computer and use it in GitHub Desktop.
Save meehow/854669 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import sys
def primes(n):
if 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
return [2] + [ x for x in s if x ]
if __name__ == '__main__':
primes_list = primes(int(sys.argv[1]))
print len(primes_list), primes_list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment