1 / 6
May 2020


’’’‘
nt = int(input())
x = []
for i in range(nt):
i = input()
x.append(i)
y = [c.split() for c in x]
for i1, n1 in enumerate(y):
strt = int(n1[0])
end = int(n1[1])
prime = [True for i in range(strt, end + 1)]
p = strt
while p * p <= end:
# If prime[p] is not changed, then it is a prime
if prime[p]:
# Update all multiples of p
for i in range(p * p, end + 1, p):
prime[i] = False
p += 1
for z in prime:
if prime[z]:
print(z+strt, “\n”)
print("\n")
’’’'
Why does this not work? It is always showing -

prime[i] = False
IndexError: list assignment index out of range

Please help me out here.

  • created

    May '20
  • last reply

    May '20
  • 5

    replies

  • 864

    views

  • 3

    users

  • 2

    links

You can’t start a sieve at any arbitrary number. If you give it the input 100 200, what is in the prime array?

(You need to sort out the indentation in the posted code. Please edit your post, and put three back ticks ``` on a line by themselves before and after the code.)

Edit: corrected number of back ticks

Exactly 3 [three] and not tick ' but back tick ` !
You can mark all you code and click icon </> [or Ctrl + Shift + C]