mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-31 06:33:44 +00:00
Improve code complexity for segmented sieve (#6372)
This commit is contained in:
parent
50545d10c5
commit
8b8fba3459
|
@ -15,15 +15,12 @@ def sieve(n):
|
||||||
if temp[start] is True:
|
if temp[start] is True:
|
||||||
in_prime.append(start)
|
in_prime.append(start)
|
||||||
for i in range(start * start, end + 1, start):
|
for i in range(start * start, end + 1, start):
|
||||||
if temp[i] is True:
|
temp[i] = False
|
||||||
temp[i] = False
|
|
||||||
start += 1
|
start += 1
|
||||||
prime += in_prime
|
prime += in_prime
|
||||||
|
|
||||||
low = end + 1
|
low = end + 1
|
||||||
high = low + end - 1
|
high = min(2 * end, n)
|
||||||
if high > n:
|
|
||||||
high = n
|
|
||||||
|
|
||||||
while low <= n:
|
while low <= n:
|
||||||
temp = [True] * (high - low + 1)
|
temp = [True] * (high - low + 1)
|
||||||
|
@ -41,9 +38,7 @@ def sieve(n):
|
||||||
prime.append(j + low)
|
prime.append(j + low)
|
||||||
|
|
||||||
low = high + 1
|
low = high + 1
|
||||||
high = low + end - 1
|
high = min(high + end, n)
|
||||||
if high > n:
|
|
||||||
high = n
|
|
||||||
|
|
||||||
return prime
|
return prime
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user