mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-18 01:00:15 +00:00
bc8df6de31
* [pre-commit.ci] pre-commit autoupdate updates: - [github.com/astral-sh/ruff-pre-commit: v0.2.2 → v0.3.2](https://github.com/astral-sh/ruff-pre-commit/compare/v0.2.2...v0.3.2) - [github.com/pre-commit/mirrors-mypy: v1.8.0 → v1.9.0](https://github.com/pre-commit/mirrors-mypy/compare/v1.8.0...v1.9.0) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
67 lines
1.7 KiB
Python
67 lines
1.7 KiB
Python
"""
|
|
Sieve of Eratosthones
|
|
|
|
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
|
|
Reference: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
|
|
|
|
doctest provider: Bruno Simas Hadlich (https://github.com/brunohadlich)
|
|
Also thanks to Dmitry (https://github.com/LizardWizzard) for finding the problem
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import math
|
|
|
|
|
|
def prime_sieve(num: int) -> list[int]:
|
|
"""
|
|
Returns a list with all prime numbers up to n.
|
|
|
|
>>> prime_sieve(50)
|
|
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
|
|
>>> prime_sieve(25)
|
|
[2, 3, 5, 7, 11, 13, 17, 19, 23]
|
|
>>> prime_sieve(10)
|
|
[2, 3, 5, 7]
|
|
>>> prime_sieve(9)
|
|
[2, 3, 5, 7]
|
|
>>> prime_sieve(2)
|
|
[2]
|
|
>>> prime_sieve(1)
|
|
[]
|
|
"""
|
|
|
|
if num <= 0:
|
|
msg = f"{num}: Invalid input, please enter a positive integer."
|
|
raise ValueError(msg)
|
|
|
|
sieve = [True] * (num + 1)
|
|
prime = []
|
|
start = 2
|
|
end = int(math.sqrt(num))
|
|
|
|
while start <= end:
|
|
# If start is a prime
|
|
if sieve[start] is True:
|
|
prime.append(start)
|
|
|
|
# Set multiples of start be False
|
|
for i in range(start * start, num + 1, start):
|
|
if sieve[i] is True:
|
|
sieve[i] = False
|
|
|
|
start += 1
|
|
|
|
for j in range(end + 1, num + 1):
|
|
if sieve[j] is True:
|
|
prime.append(j)
|
|
|
|
return prime
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print(prime_sieve(int(input("Enter a positive integer: ").strip())))
|