-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsegmented_sieve.py
54 lines (42 loc) · 1.17 KB
/
segmented_sieve.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from math import sqrt
prime = []
def simple_sieve(l,n):
temp = [True for i in range(n)]
temp[0],temp[1] = False,False
p = 2
while p*p<=n:
if temp[p]:
i = p*p
while i<n:
temp[i] = False
i += p
p += 1
for i in range(len(temp)):
if temp[i]:
if i >= l:
print(i,end = " ")
prime.append(i)
def segmented_sieve(l,n):
limit = int(sqrt(n)) + 1
simple_sieve(l,limit)
low = limit
high = limit*2
while low<n:
if high >= n:
high = n
mark = [True for _ in range(limit+2)]
for i in range(len(prime)):
low_lim = (low//prime[i]) * prime[i]
if low_lim<low:
low_lim += prime[i]
for j in range(low_lim,high+1,prime[i]):
mark[j-low] = False
for i in range(low,high+1):
if mark[i-low]:
if i>=l and i<=n:
print(i,end= " ")
low += limit
high += limit
l,r = list(map(int,input().split()))
segmented_sieve(l,r)
print()