Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.55 KB | None | 0 0
  1. def eratosthenes_sieve(n):
  2.     # Create a candidate list within which non-primes will be
  3.     # marked as None; only candidates below sqrt(n) need be checked.
  4.     candidates = list(range(n+1))
  5.     fin = int(n**0.5)
  6.  
  7.     # Loop over the candidates, marking out each multiple.
  8.     for i in range(2, fin+1):
  9.         if candidates[i]:
  10.             # from 2i in steps of i, set each to None.
  11.             candidates[2*i::i] = [None] * len(candidates[2*i::i])
  12.  
  13.     # Filter out non-primes and return the list.
  14.     return [i for i in candidates[2:] if i]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement