Skip to content

Instantly share code, notes, and snippets.

@rinfz
Created February 21, 2015 21:03
Show Gist options
  • Save rinfz/f093eb75ab2b1898e3cf to your computer and use it in GitHub Desktop.
Save rinfz/f093eb75ab2b1898e3cf to your computer and use it in GitHub Desktop.
erat
def sieve(limit):
"""
Performs the Sieve of Eratosthenes
:return: A list of prime numbers less than a given limit
"""
numbers = [True] * (limit + 1)
for i in range(2, int(math.sqrt(limit))):
if numbers[i]:
j_increment = lambda j: [int(math.pow(j, 2) + (n * j)) for n in
itertools.takewhile(lambda x: int(math.pow(j, 2) + (x * j)) <= limit,
range(0, limit))]
for j in j_increment(i):
numbers[j] = False
return [i for i, j in enumerate(numbers) if j and i > 1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment