Skip to content

Commit 8b8fba3

Browse files
authored
Improve code complexity for segmented sieve (#6372)
1 parent 50545d1 commit 8b8fba3

File tree

1 file changed

+3
-8
lines changed

1 file changed

+3
-8
lines changed

Diff for: maths/segmented_sieve.py

+3-8
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,12 @@ def sieve(n):
1515
if temp[start] is True:
1616
in_prime.append(start)
1717
for i in range(start * start, end + 1, start):
18-
if temp[i] is True:
19-
temp[i] = False
18+
temp[i] = False
2019
start += 1
2120
prime += in_prime
2221

2322
low = end + 1
24-
high = low + end - 1
25-
if high > n:
26-
high = n
23+
high = min(2 * end, n)
2724

2825
while low <= n:
2926
temp = [True] * (high - low + 1)
@@ -41,9 +38,7 @@ def sieve(n):
4138
prime.append(j + low)
4239

4340
low = high + 1
44-
high = low + end - 1
45-
if high > n:
46-
high = n
41+
high = min(high + end, n)
4742

4843
return prime
4944

0 commit comments

Comments
 (0)