Skip to content

Instantly share code, notes, and snippets.

@jargnar
Last active April 18, 2016 04:03
Show Gist options
  • Save jargnar/62551403a5892cca8965 to your computer and use it in GitHub Desktop.
Save jargnar/62551403a5892cca8965 to your computer and use it in GitHub Desktop.
Generate prime numbers up to N using the Sieve of Eratosthenes
'''
Get all primes till N (using Sieve of Eratosthenes)
The MIT License (MIT)
Copyright (c) 2016 Suhas S G <[email protected]>
'''
import math
def primes(n):
s, s[:2] = [True] * n, [False, False]
sqrtn = int(math.sqrt(n) + 1)
for i in range(sqrtn):
if s[i]:
for j in range(i*i, n+1, i):
s[j] = False
return [i for i, p in enumerate(s) if p]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment