mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
c909da9b08
* pre-commit: Upgrade psf/black for stable style 2023 Updating https://github.com/psf/black ... updating 22.12.0 -> 23.1.0 for their `2023 stable style`. * https://github.com/psf/black/blob/main/CHANGES.md#2310 > This is the first [psf/black] release of 2023, and following our stability policy, it comes with a number of improvements to our stable style… Also, add https://github.com/tox-dev/pyproject-fmt and https://github.com/abravalheri/validate-pyproject to pre-commit. I only modified `.pre-commit-config.yaml` and all other files were modified by pre-commit.ci and psf/black. * [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>
46 lines
1020 B
Python
46 lines
1020 B
Python
"""Segmented Sieve."""
|
|
|
|
import math
|
|
|
|
|
|
def sieve(n: int) -> list[int]:
|
|
"""Segmented Sieve."""
|
|
in_prime = []
|
|
start = 2
|
|
end = int(math.sqrt(n)) # Size of every segment
|
|
temp = [True] * (end + 1)
|
|
prime = []
|
|
|
|
while start <= end:
|
|
if temp[start] is True:
|
|
in_prime.append(start)
|
|
for i in range(start * start, end + 1, start):
|
|
temp[i] = False
|
|
start += 1
|
|
prime += in_prime
|
|
|
|
low = end + 1
|
|
high = min(2 * end, n)
|
|
|
|
while low <= n:
|
|
temp = [True] * (high - low + 1)
|
|
for each in in_prime:
|
|
t = math.floor(low / each) * each
|
|
if t < low:
|
|
t += each
|
|
|
|
for j in range(t, high + 1, each):
|
|
temp[j - low] = False
|
|
|
|
for j in range(len(temp)):
|
|
if temp[j] is True:
|
|
prime.append(j + low)
|
|
|
|
low = high + 1
|
|
high = min(high + end, n)
|
|
|
|
return prime
|
|
|
|
|
|
print(sieve(10**6))
|