1 / 6
Feb 2016

The following algorithm seems to be working fine, but when i submit it, its giving NZEC time error.

def primes(a,n):
    if n<2:
        return []
    sieve=[True]*(n+1)
    sieve[0]=False
    sieve[1]=False
    for i in range(2,int(n**0.5)+1):
        if sieve[i]:
            for j in range(i**2,n+1,i):
                sieve[j]=False
    for i in range(a,n+1):
        if sieve[i]:
            print i

n=int(raw_input())
a=list()
for i in range(n):
    a.append(raw_input().split())
for i in range(n):
    primes(int(a[i][0]),int(a[i][1]))
    if i!=n-1:
        print ""

thanks in advance

  • created

    Feb '16
  • last reply

    Dec '16
  • 5

    replies

  • 2.9k

    views

  • 3

    users

  • 1

    link

Which test cases did you try? And what do you think is the "worst" case your code must handle?

I tried the cases: 1 1, 1 2, 2 2, 2 3, 3 3, 6 6, 1 100, 1 1000, etc
I dont know the worst case, maybe 10^3 10^6

At SPOJ you can be pretty sure that all corner cases (max, min, or otherwise challenging cases) will be checked. So a case with n = 1,000,000,000 (10^9) is very likely and has to be considered by your code design.

10 months later

can you please hepl me?, I'm getting wrong answer but I do not understand why:
groups=int(input())
for a in range(groups):
inforaw=input()
inforaw.strip()
info=inforaw.split(" ")
zac=int(info[0])
kon=int(info[1])+1
for i in range (zac,kon):
if i > 1:
if i == 2:
print(i)
for j in range (2,i):
if i%j == 0:
prime=False
break
else:
print(i)
break
if a != groups:
print("")