2019-07-26 10:25:38 +00:00
|
|
|
"""
|
|
|
|
Sieve of Eratosthones
|
|
|
|
|
2020-06-16 08:09:19 +00:00
|
|
|
The sieve of Eratosthenes is an algorithm used to find prime numbers, less than or
|
|
|
|
equal to a given value.
|
|
|
|
Illustration:
|
|
|
|
https://upload.wikimedia.org/wikipedia/commons/b/b9/Sieve_of_Eratosthenes_animation.gif
|
2019-07-26 10:25:38 +00:00
|
|
|
Reference: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
|
|
|
|
|
|
|
|
doctest provider: Bruno Simas Hadlich (https://github.com/brunohadlich)
|
2020-11-23 05:37:42 +00:00
|
|
|
Also thanks to Dmitry (https://github.com/LizardWizzard) for finding the problem
|
2019-07-26 10:25:38 +00:00
|
|
|
"""
|
2024-03-13 06:52:41 +00:00
|
|
|
|
2021-09-07 11:37:03 +00:00
|
|
|
from __future__ import annotations
|
2019-07-10 20:09:24 +00:00
|
|
|
|
2018-10-19 12:48:28 +00:00
|
|
|
import math
|
2019-07-10 20:09:24 +00:00
|
|
|
|
2019-07-26 10:25:38 +00:00
|
|
|
|
2021-09-07 11:37:03 +00:00
|
|
|
def prime_sieve(num: int) -> list[int]:
|
2019-07-26 10:25:38 +00:00
|
|
|
"""
|
|
|
|
Returns a list with all prime numbers up to n.
|
2020-05-22 06:10:11 +00:00
|
|
|
|
2020-11-23 05:37:42 +00:00
|
|
|
>>> prime_sieve(50)
|
2019-07-26 10:25:38 +00:00
|
|
|
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
|
2020-11-23 05:37:42 +00:00
|
|
|
>>> prime_sieve(25)
|
2019-07-26 10:25:38 +00:00
|
|
|
[2, 3, 5, 7, 11, 13, 17, 19, 23]
|
2020-11-23 05:37:42 +00:00
|
|
|
>>> prime_sieve(10)
|
2019-07-26 10:25:38 +00:00
|
|
|
[2, 3, 5, 7]
|
2020-11-23 05:37:42 +00:00
|
|
|
>>> prime_sieve(9)
|
2019-07-26 10:25:38 +00:00
|
|
|
[2, 3, 5, 7]
|
2020-11-23 05:37:42 +00:00
|
|
|
>>> prime_sieve(2)
|
2019-07-26 10:25:38 +00:00
|
|
|
[2]
|
2020-11-23 05:37:42 +00:00
|
|
|
>>> prime_sieve(1)
|
2019-07-26 10:25:38 +00:00
|
|
|
[]
|
|
|
|
"""
|
|
|
|
|
2020-11-23 05:37:42 +00:00
|
|
|
if num <= 0:
|
2023-05-26 07:34:17 +00:00
|
|
|
msg = f"{num}: Invalid input, please enter a positive integer."
|
|
|
|
raise ValueError(msg)
|
2020-11-23 05:37:42 +00:00
|
|
|
|
|
|
|
sieve = [True] * (num + 1)
|
2018-10-19 12:48:28 +00:00
|
|
|
prime = []
|
|
|
|
start = 2
|
2020-11-23 05:37:42 +00:00
|
|
|
end = int(math.sqrt(num))
|
2019-07-26 10:25:38 +00:00
|
|
|
|
2019-07-10 20:09:24 +00:00
|
|
|
while start <= end:
|
2019-07-26 10:25:38 +00:00
|
|
|
# If start is a prime
|
2020-11-23 05:37:42 +00:00
|
|
|
if sieve[start] is True:
|
2018-10-19 12:48:28 +00:00
|
|
|
prime.append(start)
|
2019-07-26 10:25:38 +00:00
|
|
|
|
|
|
|
# Set multiples of start be False
|
2020-11-23 05:37:42 +00:00
|
|
|
for i in range(start * start, num + 1, start):
|
|
|
|
if sieve[i] is True:
|
|
|
|
sieve[i] = False
|
2019-07-26 10:25:38 +00:00
|
|
|
|
2018-10-19 12:48:28 +00:00
|
|
|
start += 1
|
2019-07-10 20:09:24 +00:00
|
|
|
|
2020-11-23 05:37:42 +00:00
|
|
|
for j in range(end + 1, num + 1):
|
|
|
|
if sieve[j] is True:
|
2018-10-19 12:48:28 +00:00
|
|
|
prime.append(j)
|
2019-07-10 20:09:24 +00:00
|
|
|
|
2018-10-19 12:48:28 +00:00
|
|
|
return prime
|
|
|
|
|
2019-07-10 20:09:24 +00:00
|
|
|
|
2019-07-21 06:16:28 +00:00
|
|
|
if __name__ == "__main__":
|
2020-11-23 05:37:42 +00:00
|
|
|
print(prime_sieve(int(input("Enter a positive integer: ").strip())))
|