Last active
April 18, 2016 04:03
-
-
Save jargnar/62551403a5892cca8965 to your computer and use it in GitHub Desktop.
Generate prime numbers up to N using the Sieve of Eratosthenes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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