//Segmented sieve
from math import floor,sqrt
def simple(limit,primes):
mark=[False]*(limit+1)
for i in range(2,limit+1):
if not mark[i]:
primes.append(i)
for j in range(i,limit+1,i):
mark[j]=True
def segsieve(low,high):
limit=floor(sqrt(high))+1
primes=[]
simple(limit,primes)
n=high-low+1
mark=[False]*(n+1)
for i in range(len(primes)):
lolim=floor(low/primes[i])*primes[i]
if lolim<low:
lolim+=primes[i]
if lolim==primes[i]:
lolim+=primes[i]
for j in range(lolim,high+1,primes[i]):
mark[j-low]=True
for i in range(low,high+1):
if not mark[i-low]:
print(i,end=" ")
if __name__ == '__main__':
segsieve(10,20)
0 Comments