My Code uses 2049M of memory (as per SPOJ) and get the error 'Runtime Error (SIGKILL), I know that the error is due to my code consuming more memory than allowed by SPOJ, but after searching online I am not able to fix it.
Please, look at my code and guide me.
def sieve(arr,a,b):
arr[0] = 0
arr[1] = 0
for i in range(2,int(b+1/2)): #Instead of checking till n, we can check till n/2 <Read Why?>
if arr[i] == 1: #If a number is prime, then it's multiples will be non prime.
for i in range(i ** 2,int(b+1), i):
arr[i] = 0 #Setting multiples of prime number to non-prime.
for i in range(a,int(b+1)):
if arr[i] == 1:
print(i)
t = int(input())
while t > 0:
t = t - 1
a,b = input().split(' ')
a = int(a)
b = int(b)
global arr
arr = [1]*int(b+1)
sieve(arr,a,b)
Thank You.
created
last reply
- 1
reply
- 581
views
- 2
users
- 2
links