Created
April 14, 2020 21:09
-
-
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)
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 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