Improve code complexity for segmented sieve (#6372)

This commit is contained in:
Daniel Pustotin 2022-10-02 19:35:02 +03:00 committed by GitHub
parent 50545d10c5
commit 8b8fba3459
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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