0

I am trying to solve the prime generator problem PRIME1 in spoj.com but getting runtime error(NZEC). The problem needs to take a number of test cases and for each test case, it needs to input two numbers in a line for each different test case and then finally print the output as the prime numbers in the range of each number The error is Runtime error time: 0.01 memory: 7736 signal:-1

    # your code goes here

def is_prime(x):
    if x<2:
        return False
    if x==2:
        return True
    for y in range(2,x):
        if x%y==0:
            return False
    return True
t=int(raw_input())
mylist=[]

for i in range(0,t):
    a=raw_input()
    a=a.split(' ')
    mylist.append(int(a[0]))
    mylist.append(int(a[1]))
k=0
while k<len(mylist):
    c=mylist[k]
    k+=1
    d=mylist[k]
    k+=1
    for z in range(c,d+1):
        if is_prime(z):
            print z
    print

2 Answers2

1

On running this on python 2.7.9, I found that there is only one error that you are using t in range(0, t) but t is string here, because our raw_input() method reads input and returns string. That raises in Python parlance. To remove this we should have to type cast the input we got. Like, t = int(raw_input()). And this will result in t as an integer.

For info about raw_input() follow: https://docs.python.org/2/library/functions.html#raw_input

For reading integer in python you can follow this post on stackoverflow.

Community
  • 1
  • 1
Paritosh
  • 485
  • 5
  • 12
0

Your issue is raw_input() returns string , not integer. But you are trying to use it directly in your range() function - for i in range(0,t) . range() function only accepts integers as arguments, so you need to convert your input into int before using in range.

t=int(raw_input())
Anand S Kumar
  • 76,986
  • 16
  • 159
  • 156