oleksii
5/22/2016 - 11:58 PM

sieve_of_eratosthenes.py

def sieve(n):
  candidates = set(range(2, n + 1))
  iterable = (number for number in xrange(2, int(math.sqrt(n)) + 1) if number in candidates)
        
  for prime in iterable:
    divisible = prime * 2
    while divisible <= n:
      if divisible in candidates: candidates.remove(divisible)
      divisible += prime
      
  for number in xrange(2, n + 1):
      if number in candidates: yield number