qyliang/CodeNanoFix huggingface dataset:
Buggy code:
def sieve(n):
    is_prime = [True for _ in range(n+1)]
    is_prime[0] = False
    
    for i in range(2, n+1):
        if is_prime[i-1]:
            j = 2 * i
            while j <= n:
                is_prime[j-1] = False
                j += i
    table = [ i for i in range(n+1) if is_prime[i-1]]
    return table

N = int(input())
L = [list(map(int, input().split())) for _ in range(N)]

prime = sieve(10 ** 5)

result = []
            
for i in range(N):
    res = 0
    for j in range(L[i][0], L[i][1]+1):
        if j in prime and (j+1)//2:
            res += 1
    result.append(res)

for res in result:
    print(res)

Fixed code:
def sieve(n):
    is_prime = [True for _ in range(n+1)]
    is_prime[0] = False
    is_prime[1] = False
    
    for i in range(2, n+1):
        if is_prime[i]:
            j = 2 * i
            while j <= n:
                is_prime[j] = False
                j += i
    table = [ i for i in range(n+1) if is_prime[i]]
    return table, is_prime

N = int(input())
L = [list(map(int, input().split())) for _ in range(N)]

prime, flag = sieve(10 ** 5)
number = [0 for i in range(10 ** 5+1)]

for i in range(2, 10 ** 5):
    if flag[i] and flag[(i+1)//2]:
        number[i] += 1
        
for i in range(1, 10 ** 5):
    number[i] += number[i-1]
        
result = []
            
for i in range(N):
    res = number[L[i][1]] - number[L[i][0]-1]
    result.append(res)

for res in result:
    print(res)
Buggy code:
q=int(input())

#prime table
prime=[0 for i in range(10**5+5)]
for i in range(2,10**5+5):
  j=1
  if not prime[i]:
    while j*i<=10**5+4:
      prime[j*i]=i
      j+=1

like2017=[0 for i in range(10**5+5)]
for i in range(3,10**5+5,2):
  j=(i+1)//2
  if i==prime[i] and j==prime[j]:
    like2017[i]=1

like2017sum=[0 for i in range(10**5+5)]
for i in range(10**5+4):
  like2017sum[i+1]=like2017sum[i]+like2017[i+1]

for i in range(q):
  l,r=map(int,input().split())
  print(like2017sum[r]-like2017sum[l]+1)

Fixed code:
q=int(input())

#prime table
prime=[0 for i in range(10**5+5)]
for i in range(2,10**5+5):
  j=1
  if not prime[i]:
    while j*i<=10**5+4:
      prime[j*i]=i
      j+=1

like2017=[0 for i in range(10**5+5)]
for i in range(3,10**5+5,2):
  j=(i+1)//2
  if i==prime[i] and j==prime[j]:
    like2017[i]=1

like2017sum=[0 for i in range(10**5+5)]
for i in range(10**5+4):
  like2017sum[i+1]=like2017sum[i]+like2017[i+1]
#print(like2017[0:54])
#print(like2017sum[0:80])
for i in range(q):
  l,r=map(int,input().split())
  if like2017[l]==1:
    alpha=1
  else:
    alpha=0
  print(like2017sum[r]-like2017sum[l]+alpha)
Buggy code:
q = int(input())
l, r = [0]*q, [0]*q
for i in range(q):
    l[i], r[i] = map(int, input().split())

MAX = 101010
is_prime = [1]*MAX
is_prime[0] = 0
is_prime[1] = 0
for i in range(2,MAX):
    if not is_prime[i]: 
        continue
    for j in range(i*2, MAX, i):
        is_prime[j] = 0

a = [0]*MAX
for i in range(MAX,,2):  # ,,2
    # if i%2 == 0: 
    #     continue
    if is_prime[i] and is_prime[(i+1)//2]: 
        a[i] = 1

s = [0]*(MAX+1)
for i in range(MAX): 
    s[i+1] = s[i] + a[i]

for i in range(q):
    print(s[r[i]+1]-s[l[i]])

Fixed code:
q = int(input())
l, r = [0]*q, [0]*q
for i in range(q):
    l[i], r[i] = map(int, input().split())

MAX = 101010
is_prime = [1]*MAX
is_prime[0] = 0
is_prime[1] = 0
for i in range(2,MAX):
    if not is_prime[i]: 
        continue
    for j in range(i*2, MAX, i):
        is_prime[j] = 0

a = [0]*MAX
for i in range(MAX):  # ,,2
    if i%2 == 0: 
        continue
    if is_prime[i] and is_prime[(i+1)//2]: 
        a[i] = 1

s = [0]*(MAX+1)
for i in range(MAX): 
    s[i+1] = s[i] + a[i]

for i in range(q):
    print(s[r[i]+1]-s[l[i]])
Buggy code:
import math


def is_prime(n):
    if n == 1:
        return False

    for k in range(2, int(math.sqrt(n)) + 1):
        if n % k == 0:
            return False
    return True


q = int(input())
lr = []
rmax = 0
for _ in range(q):
    l, r = map(int, input().split())
    lr.append([l, r])
    rmax = max(rmax, r)

csum = [0]
for i in range(1, rmax+1):
    if is_prime(i) and is_prime((i+1)/2):
        csum.append(csum[i-1]+1)
    else:
        csum.append(csum[i-1])

print(csum)
for i in range(q):
    if lr[i][1] == lr[i][0]:
        print(csum[lr[i][1]]-csum[lr[i][0]-1])
        #print(csum[lr[i][1]]-csum[lr[i][0]], lr[i][0],lr[i][1], csum[lr[i][0]], csum[lr[i][1]])
    else:
        print(csum[lr[i][1]]-csum[lr[i][0]-1])
        #print(csum[lr[i][1]]-csum[lr[i][0]-1], lr[i][0],lr[i][1], csum[lr[i][0]], csum[lr[i][1]])


Fixed code:
import math


def is_prime(n):
    if n == 1:
        return False

    for k in range(2, int(math.sqrt(n)) + 1):
        if n % k == 0:
            return False
    return True


q = int(input())
lr = []
rmax = 0
for _ in range(q):
    l, r = map(int, input().split())
    lr.append([l, r])
    rmax = max(rmax, r)

csum = [0]
for i in range(1, rmax+1):
    if is_prime(i) and (i+1) % 2 == 0 and is_prime((i+1)//2):
        csum.append(csum[i-1]+1)
    else:
        csum.append(csum[i-1])

#print(csum)
for i in range(q):
    if lr[i][1] == lr[i][0]:
        print(csum[lr[i][1]]-csum[lr[i][0]-1])
        #print(csum[lr[i][1]]-csum[lr[i][0]], lr[i][0],lr[i][1], csum[lr[i][0]], csum[lr[i][1]])
    else:
        print(csum[lr[i][1]]-csum[lr[i][0]-1])
        #print(csum[lr[i][1]]-csum[lr[i][0]-1], lr[i][0],lr[i][1], csum[lr[i][0]], csum[lr[i][1]])
Buggy code:
n = int(input())
lr = [None]*100000
rmax = 0
for i in range(n):
    lr[i]= list(map(int,input().split()))
    if lr[i][1] > rmax:
        rmax = lr[i][1]

p = [1]*(rmax+1)
p[0]=0
p[1]=0
        

for j in range(2,int(math.rmax**0.5+1)):
    if p[j] == 0:
        continue
    for i in range(2,rmax+1):
        if i!=j and i%j == 0:
            p[i] = 0
for i in range(rmax, 1,-2):
    if p[int((i+1)/2)] == 0:
        p[i] = 0
p[2] = 0
for i in range(2,rmax):
    p[i+1] += p[i]  

for i in range(n):
    print(p[lr[i][1]] - p[lr[i][0]-1])

Fixed code:
n = int(input())
lr = [None]*100000
rmax = 0
for i in range(n):
    lr[i]= list(map(int,input().split()))
    if lr[i][1] > rmax:
        rmax = lr[i][1]

p = [1]*(rmax+1)
p[0]=0
p[1]=0
        

for j in range(2,int(rmax**0.5+1)):
    if p[j] == 0:
        continue
    for i in range(2,rmax+1):
        if i!=j and i%j == 0:
            p[i] = 0
for i in range(rmax, 1,-2):
    if p[int((i+1)/2)] == 0:
        p[i] = 0
p[2] = 0
for i in range(2,rmax):
    p[i+1] += p[i]  

for i in range(n):
    print(p[lr[i][1]] - p[lr[i][0]-1])
Buggy code:
import math
n = int(input())
lr = [None]*10000
BT = False
rmax = 0
for i in range(n):
    lr[i]= list(map(int,input().split()))
    if lr[i][1] > rmax:
        rmax = lr[i][1]

p = [1]*(rmax+1)
p[0]=0
p[1]=0
        

for j in range(2,int(math.sqrt(rmax)+1)):
    if p[j] == 0:
        continue
    for i in range(2,rmax+1):
        if i!=j and i%j == 0:
            p[i] = 0
for i in range(rmax, 1,-2):
    if p[(i+1)//2] == 0:
        p[i] = 0
for i in range(2,rmax):
    p[i+1] +=p[i]
  

for i in range(n):
    print(p[lr[i][1]] - p[lr[i][0]-1])

Fixed code:
import math
n = int(input())
lr = [None]*100000
rmax = 0
for i in range(n):
    lr[i]= list(map(int,input().split()))
    if lr[i][1] > rmax:
        rmax = lr[i][1]

p = [1]*(rmax+1)
p[0]=0
p[1]=0
        

for j in range(2,int(math.sqrt(rmax)+1)):
    if p[j] == 0:
        continue
    for i in range(2,rmax+1):
        if i!=j and i%j == 0:
            p[i] = 0
for i in range(rmax, 1,-2):
    if p[int((i+1)/2)] == 0:
        p[i] = 0
p[2] = 0
for i in range(2,rmax):
    p[i+1] += p[i]  

for i in range(n):
    print(p[lr[i][1]] - p[lr[i][0]-1])
Buggy code:
def calc_prime(n):
    primes = set(list(range(3, n, 2)))
    primes.add(2)
    for a in range(3, n, 2):
        for i in range(a, n):
            x = a * i
            if x >= n:
                break
            if x in primes:
                primes.remove(x)

    return primes


def collect_likes(ps):
    likes = set([p for p in ps if (p + 1) / 2 in ps])
    return likes


def line(xs, n):
    ps = list(xs)
    ps.sort()
    cs = [0] * n
    s = 0
    for i in range(n):
        if i in xs:
            cs[i] = s
            s += 1
        else:
            cs[i] = s

    return cs


def main():
    n = 10 ** 5
    primes = calc_prime(n)
    likes = collect_likes(primes)
    cums = line(likes, n)
    #print(sorted(list(primes))[:100])
    print(sorted(list(likes))[:100])
    #print(cums[:10])

    q = int(input())
    for _ in range(q):
        l, r = [int(x) for x in input().split()]
        ans = cums[r + 1] - cums[l]
        print(ans)


main()


Fixed code:
def calc_prime(n):
    primes = set(list(range(3, n, 2)))
    primes.add(2)
    for a in range(3, n, 2):
        for i in range(a, n):
            x = a * i
            if x >= n:
                break
            if x in primes:
                primes.remove(x)

    return primes


def collect_likes(ps):
    likes = set([p for p in ps if (p + 1) / 2 in ps])
    return likes


def line(xs, n):
    ps = list(xs)
    ps.sort()
    cs = [0] * n
    s = 0
    for i in range(n):
        if i in xs:
            cs[i] = s
            s += 1
        else:
            cs[i] = s

    return cs


def main():
    n = 10 ** 5 + 9
    primes = calc_prime(n)
    likes = collect_likes(primes)
    cums = line(likes, n)
    #print(sorted(list(primes))[:100])
    #print(sorted(list(likes))[:100])
    #print(cums[:10])

    q = int(input())
    for _ in range(q):
        l, r = [int(x) for x in input().split()]
        ans = cums[r + 1] - cums[l]
        print(ans)


main()
Buggy code:
import math
def is_prime(n):
    if n % 2 == 0 and n > 2:
        return False
    for i in range(3, int(math.sqrt(n)) + 1, 2):
        if n % i == 0:
            return False
    return True

Q= 100000

primes = [is_prime(n) and is_prime((n+1) / 2) for n in range(0, Q+1)]

accu=[0] * (Q+1)
for i in range(1, Q+1):
    #print(i)
    accu[i] = accu[i-1]
    if primes[i]:
        accu[i] = accu[i]+ 1

n = int(input())
for k in range(n):
    l,r = map(int, input().strip().split(' '))
    print("%d" % (accu[r] - accu[l-1]))


Fixed code:
import math
def is_prime(n):
    if n < 2:
        return False
    if n % 2 == 0 and n > 2:
        return False
    for i in range(3, int(math.sqrt(n)) + 1, 2):
        if n % i == 0:
            return False
    return True

Q= 100000

primes = [is_prime(n) and is_prime((n+1) / 2) for n in range(0, Q+1)]

accu=[0] * (Q+1)
for i in range(1, Q+1):
    #print(i)
    accu[i] = accu[i-1]
    if primes[i]:
        accu[i] = accu[i]+ 1

n = int(input())
for k in range(n):
    l,r = map(int, input().strip().split(' '))
    print("%d" % (accu[r] - accu[l-1]))
Buggy code:
def is_prime(t):
  i=2
  while i**2<=t:
    if t%i==0:
      return 0
    i+=1
  return 1
q=int(input())
lr=[list(map(int,input().split())) for _ in range(q)]
n=10**5
p=[0]*(n+1)
d=[]
for i in range(2,n):
  if is_prime(i):
    d.append(i)
  if i in d and (i+1)//2 in d:
    p[i]=p[i-1]+1
  else:
    p[i]=p[i-1]
for l,r in lr:
  print(p[r]-p[l-1])



Fixed code:
def is_prime(t):
  i=2
  while i**2<=t:
    if t%i==0:
      return 0
    i+=1
  return 1
q=int(input())
lr=[list(map(int,input().split())) for _ in range(q)]
n=10**5
p=[0]*(n+1)
d=set([])
for i in range(2,n):
  if is_prime(i):
    d.add(i)
  if i in d and (i+1)//2 in d:
    p[i]=p[i-1]+1
  else:
    p[i]=p[i-1]
for l,r in lr:
  print(p[r]-p[l-1])
Buggy code:
Q = int(input())
ans = 0
INF = 10 ** 5
is_prime = [1] * INF
rui = [0] * (INF + 1)
def eratorasu():
    is_prime[0] = 0
    is_prime[1] = 0
    for i in range(2, INF):
        if is_prime[i] == 0:
            continue
        for j in range(i * 2, INF, i):
            is_prime[j] = 0
    return

eratorasu()
sosu = [0] * INF
for i in range(INF):
    if is_prime[i] == 1 and is_prime[(i + 1) // 2] == 1:
        sosu[i] = 1

rui = [0] * (INF + 1)
for i in range(INF):
    rui[i + 1] = rui[i] + sosu[i]

for i in range(Q):
    l, r = map(int, input().split())
    ans = rui[r] - rui[l - 1]
    print(ans)

Fixed code:
Q = int(input())
ans = 0
INF = 10 ** 5
is_prime = [1] * INF
rui = [0] * (INF + 1)
def eratorasu():
    is_prime[0] = 0
    is_prime[1] = 0
    for i in range(2, INF):
        if is_prime[i] == 0:
            continue
        for j in range(i * 2, INF, i):
            is_prime[j] = 0
    return

eratorasu()
sosu = [0] * INF
for i in range(INF):
    if is_prime[i] == 1 and is_prime[(i + 1) // 2] == 1:
        sosu[i] = 1

rui = [0] * (INF + 1)
for i in range(INF):
    rui[i + 1] = rui[i] + sosu[i]

for i in range(Q):
    l, r = map(int, input().split())
    ans = rui[r + 1] - rui[l]
    print(ans)
Buggy code:
def sieve_of_eratosthenes(ub):
    # O(n loglog n) implementation.
    # returns prime numbers <= ub.
    if ub < 2:
        return []
    prime = [True]*(ub+1)
    prime[0] = False
    prime[1] = False
    p = 2
    while p*p <= ub:
        if prime[p]:
            # don't need to check p+1 ~ p^2-1 (they've already been checked).
            for i in range(p*p, ub+1, p):
                prime[i] = False
        p += 1
    return [i for i in range(ub+1) if prime[i]]

Q = int(input())
primes = sieve_of_eratosthenes(10**5)
cum_sum = [0]*(10**5+1)
for i in range(1, 10**5+1):
    if i in primes and (i+1) // 2 in primes:
        cum_sum[i] = cum_sum[i-1] + 1
    else:
        cum_sum[i] = cum_sum[i-1]

for _ in range(Q):
    l, r = map(int, input().split())
    ans = cum_sum[r] - cum_sum[l-1]
    print(ans)

Fixed code:
def sieve_of_eratosthenes(ub):
    # O(n loglog n) implementation.
    # returns prime numbers <= ub.
    if ub < 2:
        return []
    prime = [True]*(ub+1)
    prime[0] = False
    prime[1] = False
    p = 2
    while p*p <= ub:
        if prime[p]:
            # don't need to check p+1 ~ p^2-1 (they've already been checked).
            for i in range(p*p, ub+1, p):
                prime[i] = False
        p += 1
    return prime

Q = int(input())
primes = sieve_of_eratosthenes(10**5)
cum_sum = [0]*(10**5+1)
for i in range(1, 10**5+1):
    if primes[i] and primes[(i+1) // 2]:
        cum_sum[i] = cum_sum[i-1] + 1
    else:
        cum_sum[i] = cum_sum[i-1]

for _ in range(Q):
    l, r = map(int, input().split())
    ans = cum_sum[r] - cum_sum[l-1]
    print(ans)
Buggy code:
f = [False for i in range(100001)]
c = [0 for i in range(100001)]

for i in xrange(2, 100001):
    if not f[i]:
        for j in range(2*i, 100001, i):
            f[j] = True
for i in xrange(3, 100001, 2):
    if not f[i] and not f[int((i+1)/2)]:
        c[i] += 1
for i in xrange(3, 100001):
    c[i] += c[i-1]
    
n = int(input())
while n:
    l, r = list(map(int, input().split()))
    print (c[r]-c[l-1])
    n -= 1


Fixed code:
f = [False for i in range(100001)]
c = [0 for i in range(100001)]

for i in range(2, 100001):
    if not f[i]:
        for j in range(2*i, 100001, i):
            f[j] = True
for i in range(3, 100001, 2):
    if not f[i] and not f[int((i+1)/2)]:
        c[i] += 1
for i in range(3, 100001):
    c[i] += c[i-1]
    
n = int(input())
while n:
    l, r = list(map(int, input().split()))
    print (c[r]-c[l-1])
    n -= 1
Buggy code:
n=int(input())
q=[list(map(int,input().split())) for _ in range(n)]
MAX=100010
p=[1]*MAX
p[0]=0;p[1]=0
for i in range(2,MAX):
  if not p[i]:continue
  for j in range(i*2,MAX,i):p[j]=0
a=[0]*MAX
for i in range(MAX):
  if i%2==0:continue
  if p[i] and p[int((i+1)/2)]:a[i]=1
s=[0]*(MAX+1)
for i in range(MAX):
  s[i+1]=s[i]+a[i]
for i in range(n):
  print(s[q[i][1]]-s[q[i][0]])

Fixed code:
n=int(input())
q=[list(map(int,input().split())) for _ in range(n)]
MAX=100010
p=[1]*MAX
p[0]=0;p[1]=0
for i in range(2,MAX):
  if not p[i]:continue
  for j in range(i*2,MAX,i):p[j]=0
a=[0]*MAX
for i in range(MAX):
  if i%2==0:continue
  if p[i] and p[int((i+1)/2)]:a[i]=1
s=[0]*(MAX+1)
for i in range(MAX):
  s[i+1]=s[i]+a[i]
for i in range(n):
  print(s[q[i][1]+1]-s[q[i][0]])
Buggy code:
def prime(n):
  if n==1:
    return False
  elif n==2 or n==3:
    return True
  else:
    for i in range(2,int(n**0.5)+1):
      if n%i==0:
        return False
    else:
      return True

ans=[0]*(10**5+1)
for i in range(3,10**5+1,2):
  if prime(i) and prime((i+1)//2):
    ans[i]=1
for i in range(1,10**5+1)=:
  ans[i]=ans[i]+ans[i-1]
q=int(input())
for i in range(q):
  l,r=map(int,input().split())
  print(ans[r]-ans[l-1])

Fixed code:
def prime(n):
  if n==1:
    return False
  elif n==2 or n==3:
    return True
  else:
    for i in range(2,int(n**0.5)+1):
      if n%i==0:
        return False
    else:
      return True

ans=[0]*(10**5+1)
for i in range(3,10**5+1,2):
  if prime(i) and prime((i+1)//2):
    ans[i]=1
for i in range(1,10**5+1):
  ans[i]=ans[i]+ans[i-1]
q=int(input())
for i in range(q):
  l,r=map(int,input().split())
  print(ans[r]-ans[l-1])
Buggy code:
import math
import numpy as np
Q = int(input())
LR = []
for _ in range(Q):
    LR.append(list(map(int, input().split())))

rmax = max(r for l, r in LR)
primes = np.ones(1 + rmax, int)
primes[::2] = 0
for i in range(3, 1 + int(math.sqrt(rmax)), 2):
    if primes[i]:
        primes[2*i::i] = 0
# like 2017
for i in range(rmax, 0, -2):
    if primes[i] and not primes[(i + 1) // 2]:
        primes[i] = 0
acc = np.cumsum(primes)
for l, r in LR:
    ans = acc[r] - acc[l - 1]
    print(ans)

Fixed code:
import math
import numpy as np
Q = int(input())
LR = []
for _ in range(Q):
    LR.append(list(map(int, input().split())))

rmax = max(r for l, r in LR)
primes = np.ones(1 + rmax, int)
for i in range(2, 1 + int(math.sqrt(rmax))):
    if primes[i]:
        primes[2*i::i] = 0
primes[:2] = 0
# like 2017
for i in range(rmax, 0, -2):
    if primes[i] and not primes[(i + 1) // 2]:
        primes[i] = 0
primes[2] = 0
acc = np.cumsum(primes)
for l, r in LR:
    ans = acc[r] - acc[l - 1]
    print(ans)
Buggy code:
n=[True]*100001
n[0]=n[1]=False
for i in range(2,317):
  if n[i]:
    for j in range(i**2,100000,i):
      n[j]=False
c=[0]*100002
for i in range(3,100001,2):
  c[i]=c[i]+c[i-1]
  if n[i] and n[int((i+1)/2)]:
    c[i]=c[i]+1
for i in range(int(input())):
  a=input().split(' ')
  print(c[int(a[1])]-c[int(a[0])-1])

Fixed code:
n=[True]*100001
n[0]=n[1]=False
for i in range(2,317):
  if n[i]:
    for j in range(i**2,100000,i):
      n[j]=False
c=[0]*100002
for i in range(3,100001,2):
  if n[i] and n[int((i+1)/2)]:
    c[i]=c[i]+1
for i in range(3,100001):
  c[i]=c[i]+c[i-1]
for i in range(int(input())):
  a=input().split(' ')
  print(c[int(a[1])]-c[int(a[0])-1])
Buggy code:
from itertools import accumulate
V = [1]*(10**5+1)
P = [0]*(10**5+1)
V[0] = 0
V[1] = 0
for i in range(10**5+1):
    if V[i] == 0:
        continue
    P[i] = 1
    for j in range(2,10**5+1, i):
        V[i*j] = 0
W = [0]*(10**5+1)
for i in range(10**5+1):
    if not V[i]:
        continue
    if V[(i+1)//2] == 1:
        W[i] = 1
accW = list( accumulate(W))
Q = int( input())
for i in range(Q):
    l, r = map( int, input().split())
    print(accW[r] - accW[l-1])

Fixed code:
from itertools import accumulate
V = [1]*(10**5+1)
P = [0]*(10**5+1)
V[0] = 0
V[1] = 0
for i in range(10**5+1):
    if V[i] == 0:
        continue
    P[i] = 1
    for j in range(2,10**5):
        if i*j >= 10**5:
            break
        V[i*j] = 0
W = [0]*(10**5+1)
for i in range(10**5+1):
    if not V[i]:
        continue
    if V[(i+1)//2] == 1:
        W[i] = 1
accW = list( accumulate(W))
Q = int( input())
for i in range(Q):
    l, r = map( int, input().split())
    print(accW[r] - accW[l-1])
Buggy code:
import bisect
N = 10**6
dp = [1] * (N+1)
dp[0] =  dp[1] =  0
prime = []
for i in range(2,N+1):
    if dp[i]:
        for j in range(i*i, N+1, i):
            dp[j] = 0
like2017 = [0] * (N+1)
for n in range(N+1):
    if dp[n]==0:
        continue
    query = (p+1)//2
    if dp[query]==1:
        like2017[n] = 1


for n in range(N):
    line2017[n+1] += like2017[n]
    
Q = int(input())
for q in range(Q):
    l, r = map(int,input().split())
    print (like2017[r]-like2017[l])


Fixed code:
import bisect
N = 10**6
dp = [1] * (N+1)
dp[0] =  dp[1] =  0
prime = []
for i in range(2,N+1):
    if dp[i]:
        for j in range(i*i, N+1, i):
            dp[j] = 0
like2017 = [0] * (N+1)
for n in range(N+1):
    if dp[n]==0:
        continue
    query = (n+1)//2
    if dp[query]==1:
        like2017[n] = 1


for n in range(N):
    like2017[n+1] += like2017[n]

Q = int(input())
for q in range(Q):
    l, r = map(int,input().split())
    print (like2017[r]-like2017[l-1])
Buggy code:
Q = int(input())
LR = [list(map(int, input().split())) for _ in range(Q)]
R = [r for l,r in LR]
maxR = max(R)

def primes_upto(m):
    _l = list(range(2, m+1))
    primes=[]
    while _l:
        p = _l[0]
        primes.append(p)
        _l=[n for n in _l if n%p]
    return primes

primes = set(primes_upto(maxR))

from itertools import accumulate

like2017s = [1 if n in primes and (n+1)//2 in primes else 0 for n in range(3,maxR+1,2)]
# 3, 5, 7, ...
n_like2017s = [0] + list(accumulate(like2017s))
# 1, 3, 5, 7, ...

for l, r in LR:
  print(n_like2017s[(r-1)//2] - n_like2017s[(l-1)//2-1])

Fixed code:
Q = int(input())
LR = [list(map(int, input().split())) for _ in range(Q)]
R = [r for l,r in LR]
maxR = max(R)

def primes_upto(m):
    _l = list(range(2, m+1))
    primes=[]
    while _l:
        p = _l[0]
        if p > m**0.5:
          primes.extend(_l)
          return primes
        primes.append(p)
        _l=[n for n in _l if n%p]
    return primes

primes = set(primes_upto(maxR))

from itertools import accumulate

like2017s = [1 if n in primes and (n+1)//2 in primes else 0 for n in range(3,maxR+1,2)]
# 3, 5, 7, ...
n_like2017s = [0] + list(accumulate(like2017s)) + [0]
# 1, 3, 5, 7, ...

for l, r in LR:
  print(n_like2017s[(r-1)//2] - n_like2017s[(l-1)//2-1])
Buggy code:
q=int(input())
#lr=[list(map(int,input().split())) for i in  range(q)]
MAX=10**5+1
l=[0]*MAX

def  is_prime(n):
	if n == 1:
		return  False

	for i in  range(2,int(n**0.5)+1):
		if n % i == 0:
			return  False
	return  True
cnt=0
for i in range(1, MAX, 2):
  if is_prime(i) and is_prime((i+1)/2):
    l[i]=1
    
l2=[0]*(MAX+1)
for i in range(0, MAX):
  l2[i+1]=l2[i]+l[i]
print(l2)

for i in range(q):
  l,r=map(int, input().split())
  r+=1
  print(l2[r]-l2[l])

Fixed code:
q=int(input())
#lr=[list(map(int,input().split())) for i in  range(q)]
MAX=10**5+1
l=[0]*MAX

def  is_prime(n):
	if n == 1:
		return  False

	for i in  range(2,int(n**0.5)+1):
		if n % i == 0:
			return  False
	return  True
cnt=0
for i in range(1, MAX, 2):
  if is_prime(i) and is_prime((i+1)/2):
    l[i]=1
    
l2=[0]*(MAX+1)
for i in range(0, MAX):
  l2[i+1]=l2[i]+l[i]
#print(l2)

for i in range(q):
  l,r=map(int, input().split())
  r+=1
  print(l2[r]-l2[l])
Buggy code:
from math import sqrt
M = 10**6
p = [1 for i in range(M+1)]
p[0] = 0
p[1] = 0
for x in range(2, int(sqrt(M))+1):
    if p[x]:
        for y in range(x*x, M+1, x):
            p[y] = 0

a = [0 for i in range(M+1)]
for i in range(M):
    if i%2 != 0 and p[i] and p[(i+1)//2]:
        a[i] = 1

ans = [0 for i in range(M)]
for i in range(1, M):
    ans[i] = ans[i-1] + a[i]

q = int(input())
for i in range(q):
    l, r = map(int, input().split())
    print(ans[r-1] - ans[l-1])

Fixed code:
from math import sqrt
M = 10**6
p = [1 for i in range(M+1)]
p[0] = 0
p[1] = 0
for x in range(2, int(sqrt(M))+1):
    if p[x]:
        for y in range(x*x, M+1, x):
            p[y] = 0

a = [0 for i in range(M+1)]
for i in range(M):
    if i%2 != 0 and p[i] and p[(i+1)//2]:
        a[i] = 1

ans = [0 for i in range(M)]
for i in range(1, M):
    ans[i] = ans[i-1] + a[i]

q = int(input())
for i in range(q):
    l, r = map(int, input().split())
    print(ans[r] - ans[l-1])