Last active
March 21, 2021 14:35
-
-
Save pawelrubin/8acd096d401205fba088bb0e20231848 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes Python implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Iterator | |
def sieve(n: int) -> Iterator[int]: | |
if n < 2: | |
return | |
primes = [True for _ in range(n + 1)] | |
for p in range(2, n + 1): | |
if primes[p]: | |
yield p | |
for i in range(p * p, n + 1, p): | |
primes[i] = False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment