Sieve of Erotosthenes Function to return all the prime numbers up to a number 'limit' https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes >>> prime_sieve(3) [2] >>> prime_sieve(50) [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
(limit: int)
| 20 | |
| 21 | |
| 22 | def prime_sieve(limit: int) -> list[int]: |
| 23 | """ |
| 24 | Sieve of Erotosthenes |
| 25 | Function to return all the prime numbers up to a number 'limit' |
| 26 | https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes |
| 27 | |
| 28 | >>> prime_sieve(3) |
| 29 | [2] |
| 30 | |
| 31 | >>> prime_sieve(50) |
| 32 | [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47] |
| 33 | """ |
| 34 | is_prime = [True] * limit |
| 35 | is_prime[0] = False |
| 36 | is_prime[1] = False |
| 37 | is_prime[2] = True |
| 38 | |
| 39 | for i in range(3, int(limit**0.5 + 1), 2): |
| 40 | index = i * 2 |
| 41 | while index < limit: |
| 42 | is_prime[index] = False |
| 43 | index = index + i |
| 44 | |
| 45 | primes = [2] |
| 46 | |
| 47 | for i in range(3, limit, 2): |
| 48 | if is_prime[i]: |
| 49 | primes.append(i) |
| 50 | |
| 51 | return primes |
| 52 | |
| 53 | |
| 54 | def solution(ceiling: int = 1_000_000) -> int: |