-
Notifications
You must be signed in to change notification settings - Fork 0
/
SegmentedSieve.py
34 lines (23 loc) · 1.01 KB
/
SegmentedSieve.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
def simple_sieve(n):
sieve = [True] * (n + 1)
sieve[:2] = [False, False]
for i in range(2, int(n**0.5) + 1):
if sieve[i]:
sieve[i*i:n+1:i] = [False] * len(range(i*i, n+1, i))
return [i for i, prime in enumerate(sieve) if prime]
def segmented_sieve(n):
delta = int(n**0.5) + 1
primes = simple_sieve(delta)
result = list(primes)
for m in range(delta, n + 1, delta):
segment = [True] * delta
lower_limit = m - delta + 1
for prime in primes:
start = max(prime * prime, (lower_limit + prime - 1) // prime * prime)
segment_start = start - lower_limit
segment[segment_start:m-lower_limit+1:prime] = [False] * len(range(segment_start, m-lower_limit+1, prime))
result.extend([i + lower_limit for i, is_prime in enumerate(segment) if is_prime])
return result
if __name__ == "__main__":
n = int(input("Enter the upper limit n: "))
print(segmented_sieve(n))