MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / prime_sieve

Function prime_sieve

project_euler/problem_050/sol1.py:22–51  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

20
21
22def 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
54def solution(ceiling: int = 1_000_000) -> int:

Callers 1

solutionFunction · 0.70

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected