I have several days struggling with this Prime Generator algorithm for SPOJ problem. The problem state to print at least 100000 primes from a number m,n with n<=1000000000 in 6 seconds. I have this implementation that print 100000 prime in 11.701067686080933 seconds. is it possible to beat the time restriction(6s) in Python. I feel that I miss something in my segmented sieve function , cause I just implemented it how I understand the algorithm work, maybe a change can make it better.
Some Help would appreciated here.
def sieveOfErosthen(m):
limit=m+1
prime=[True]*limit
for i in range(2,int(m**0.5)):
if prime[i]:
for x in range(i*i,limit,i):
prime[x]=False
return prime
def segmentedSieve(m,n):
limit= n+1
segment=[True]*limit
for j in range(2,int(n**0.5) ):
if sieveOfErosthen(j):
for b in range(j*(m//j),limit,j):
if b >j:
segment[b]=False
for v in range(m,limit):
if segment[v]:
print(v)
return True