Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def eratosthenes_sieve(n):
- # Create a candidate list within which non-primes will be
- # marked as None; only candidates below sqrt(n) need be checked.
- candidates = list(range(n+1))
- fin = int(n**0.5)
- # Loop over the candidates, marking out each multiple.
- for i in range(2, fin+1):
- if candidates[i]:
- # from 2i in steps of i, set each to None.
- candidates[2*i::i] = [None] * len(candidates[2*i::i])
- # Filter out non-primes and return the list.
- return [i for i in candidates[2:] if i]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement