Skip to content

Instantly share code, notes, and snippets.

@rense
Last active August 29, 2015 13:59
Show Gist options
  • Save rense/10870467 to your computer and use it in GitHub Desktop.
Save rense/10870467 to your computer and use it in GitHub Desktop.
def sieve(limit):
limit += 1
primes = []
a = [True] * limit
a[0] = a[1] = False
for (i, prime) in enumerate(a):
if not prime:
continue
primes.append(i)
for n in range(i * 2, limit, i):
a[n] = False
print primes
def sieve_two(limit):
limit += 1
primes = []
multiples = []
for i in range(2, limit):
if i in multiples:
continue
primes.append(i)
for j in range(i * 2, limit, i):
multiples.append(j)
print primes
import math
def sieve_three(n):
result = range(2, n)
i = 0
while i < len(result) and result[i] < math.sqrt(n):
x = result[i] * 2
while x < n:
if x in result:
result.remove(x)
x += result[i]
i += 1
print result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment