Buggy python submission (verdict: Time Limit Exceeded):

import math
N=int(input())

n=110000
count=0
isprime=[True]*(n+1)
isprime[0]=False
isprime[1]=False
for i in range(2,int(math.sqrt(n))+1):
    if isprime[i]==True:
        j=i+i
        while j<=n:
            isprime[j] =False
            j=j+i
for k in range(N):
    l,r=map(int,input().split())
    count=0
    for i in range(l,r+1):
        if isprime[i]==True:
            if isprime[(i+1)//2]==True:
                count+=1
    print(count)

    


Fixed submission:

import math
N=int(input())
S=[0]*100010
n=110000
count=0
isprime=[True]*(n+1)
isprime[0]=False
isprime[1]=False
for i in range(2,int(math.sqrt(n))+1):
    if isprime[i]==True:
        j=i+i
        while j<=n:
            isprime[j] =False
            j=j+i

for i in range(2,100010):
    if isprime[i]==True:
        if isprime[(i+1)//2]==True:
            count+=1
    
    S[i]=count
        
                
    

    
for k in range(N):
    l,r=map(int,input().split())
    
    print(S[r]-S[l-1])

Buggy python submission (verdict: Time Limit Exceeded):

import math
N=int(input())
S=[]
def eratos(n,l,r):
    count=0
    isprime=[True]*(n+1)
    isprime[0]=False
    isprime[1]=False
    for i in range(2,int(math.sqrt(n))+1):
        if isprime[i]==True:
            j=i+i
            while j<=n:
                isprime[j] =False
                j=j+i

    for i in range(l,r+1):
        if isprime[i]==True:
            if isprime[(i+1)//2]==True:
                count+=1
    
    return count
    
for k in range(N):
    l,r=map(int,input().split())

    print(eratos(110000,l,r))

Fixed submission:

import math
N=int(input())
S=[0]*100010
n=110000
count=0
isprime=[True]*(n+1)
isprime[0]=False
isprime[1]=False
for i in range(2,int(math.sqrt(n))+1):
    if isprime[i]==True:
        j=i+i
        while j<=n:
            isprime[j] =False
            j=j+i

for i in range(2,100010):
    if isprime[i]==True:
        if isprime[(i+1)//2]==True:
            count+=1
    
    S[i]=count
        
                
    

    
for k in range(N):
    l,r=map(int,input().split())
    
    print(S[r]-S[l-1])

Buggy python submission (verdict: Time Limit Exceeded):

import math
N=int(input())
S=[]
def eratos(n):
    isprime=[True]*(n+1)
    isprime[0]=False
    isprime[1]=False
    for i in range(2,int(math.sqrt(n))+1):
        if isprime[i]==True:
            j=i+i
            while j<=n:
                isprime[j] =False
                j=j+i

    for i in range(n+1):
        if isprime[i]==True:
            S.append(i)
    
    return S
    

T=eratos(110000)

for k in range(N):
    count=0
    l,r=map(int,input().split())

    for i in range(l,r+1):
        if i in T:
            if (i+1)//2 in T:
                count+=1
    print(count)

Fixed submission:

import math
N=int(input())
S=[0]*100010
n=110000
count=0
isprime=[True]*(n+1)
isprime[0]=False
isprime[1]=False
for i in range(2,int(math.sqrt(n))+1):
    if isprime[i]==True:
        j=i+i
        while j<=n:
            isprime[j] =False
            j=j+i

for i in range(2,100010):
    if isprime[i]==True:
        if isprime[(i+1)//2]==True:
            count+=1
    
    S[i]=count
        
                
    

    
for k in range(N):
    l,r=map(int,input().split())
    
    print(S[r]-S[l-1])