Skip to content

Instantly share code, notes, and snippets.

@ashwch
Last active December 12, 2015 09:59
Show Gist options
  • Select an option

  • Save ashwch/4756288 to your computer and use it in GitHub Desktop.

Select an option

Save ashwch/4756288 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes
"""
Implementation of Sieve of Eratosthenes in python.
Algorithm descripition : http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
"""
def primes(n):
if n<=2:
return []
sieve=[True]*(n+1)
for x in range(3,int(n**0.5)+1,2):
for y in range(3,(n//x)+1,2):
sieve[(x*y)]=False
return [2]+[i for i in range(3,n,2) if sieve[i]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment