Skip to content

Instantly share code, notes, and snippets.

@pawelrubin
Last active March 21, 2021 14:35
Show Gist options
  • Save pawelrubin/8acd096d401205fba088bb0e20231848 to your computer and use it in GitHub Desktop.
Save pawelrubin/8acd096d401205fba088bb0e20231848 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes Python implementation
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