1 / 2
Jan 2020

I have a problem about a time limit exceeded when my code is submitted though the code runs perfectly on both my IDE and ideone.com. I’ll appreciate any help to speed up my code

from sys import stdin as fp
buffer=fp.readlines()
new_buffer=[]
for i in buffer:
    i=i.strip()
    new_buffer.append(i)
newest_buffer=[]
for i in range(1,len(new_buffer)):
    newest_buffer.append(new_buffer[i].split())
for numlist in newest_buffer:
    start=int(numlist[0])
    stop=int((numlist[1]))+1
    sieve=[i for i in range(1,stop)]
    if sieve[0]==1:
        sieve.pop(0)
    for i in range(0,len(sieve)):
        j=i+1
        while j<len(sieve):
            if sieve[j]%sieve[i]==0:
                sieve.pop(j)
            else:
                j+=1
    for index in range (0,len(sieve)):
        if sieve[0]<start:
            sieve.pop(0)
    for elements in sieve:
        print(elements)
    print()
  • created

    Jan '20
  • last reply

    Jan '20
  • 1

    reply

  • 969

    views

  • 2

    users

  • 1

    like

  • 4

    links

Did you try it with a test case like below? How long did it take to run?

1
999900000 1000000000

Questions about TLE on PRIME14 have been asked many times before - look here30 or here15 for possible answers.

Or it may be that Python is just too slow for this problem, and you’d be better off with another language.