Skip to content

Instantly share code, notes, and snippets.

@bvanrijn
Created April 14, 2020 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bvanrijn/bf1440fb232ac41183d5664231c5beff to your computer and use it in GitHub Desktop.
Save bvanrijn/bf1440fb232ac41183d5664231c5beff to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes (based on https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes)
from math import floor, sqrt
from typing import List
def sieve(n: int) -> List[int]:
A: List[bool] = [True] * n
for i in range(2, floor(sqrt(n) + 1), 1):
if A[i]:
for j in range(i ** 2, n, i):
A[j] = False
primes: List[int] = []
for i, is_prime in zip(range(len(A)), A):
if is_prime:
primes.append(i)
return primes[2:]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment