Buggy python submission (verdict: Time Limit Exceeded):
import math
Q = int(input())


def prime_factorization(num):
    sqrt_num = math.sqrt(num)
    prime_numbers = []
    for i in range(2, int(sqrt_num) + 1):
        # print(num)
        while num % i == 0:
            # print(num)
            num = num / i
            prime_numbers.append(i)
    if num != 1:
        prime_numbers.append(int(num))
    return prime_numbers


for i in range(Q):
    l, r = map(int, input().split())
    count = 0
    for k in range(l, r + 1):
        if k % 2 == 1:
            # print(k)
            if len(prime_factorization(k)) == 1 and len(prime_factorization((k + 1) // 2)) == 1:
                count += 1
    print(count)


Fixed submission:
from itertools import accumulate
INF = float('inf')


def prime_numbers(n):
    if n < 2:
        return []

    m = (n + 1) // 2
    p = [True] * m
    for i in range(1, int((n ** 0.5 - 1) / 2) + 1):
        if p[i]:
            for j in range(2 * i * (i + 1), m, 2 * i + 1):
                p[j] = False

    a = [2 * i + 1 for i in range(m) if p[i]]
    a[0] = 2
    return a


N = 10 ** 5
P = set(prime_numbers(N))
# これが素数
# print(P)
P = [n for n in P if (n + 1) // 2 in P]
# 自身 + 1 //2 も素数であるもののみに絞る
# print(P)
a = [0] * (N + 1)
for n in P:
    # likenumberは1にする
    a[n] = 1
# 累積和に変換
a = list(accumulate(a))


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

Buggy python submission (verdict: Time Limit Exceeded):
import math
Q = int(input())
num_ranges = [list(map(int, input().split())) for _ in range(Q)]

primes = [2]
likes_nums = []


def is_prime(num):
    # 今まででた素数で割っていくと、自身が素数かどうかわかる
    if num == 1:
        return False
    if primes[-1] <= math.ceil(math.sqrt(num)):
        for i in range(primes[-1], math.ceil(math.sqrt(num)) + 1):
            is_prime = True
            # 今まででた素数で割っていくと、自身が素数かどうかわかる
            for p in primes:
                if i % p == 0:
                    is_prime = False
            if is_prime:
                primes.append(i)
    for p in primes:
        # 全てprimesを見るのは時間が勿体無いので、自身よりも大きいもの・もしくは同じになったら抜ける.
        # もし素数でないのであれば、p<nの間にあるはず。ない状態で、同じ数値まで来た場合も素数
        if num <= p:
            return True

        if num % p == 0:
            return False
    return True


def check(num):
    if num in likes_nums:
        return True
    if is_prime(num) and is_prime((num + 1) / 2):
        likes_nums.append(num)
        return True
    else:
        return False


for start, end in num_ranges:
    count = 0
    for i in range(start, end + 1):
        if i % 2 == 0:
            continue
        if check(i):
            # print(i)
            count += 1
    print(count)


Fixed submission:
from itertools import accumulate
INF = float('inf')


def prime_numbers(n):
    if n < 2:
        return []

    m = (n + 1) // 2
    p = [True] * m
    for i in range(1, int((n ** 0.5 - 1) / 2) + 1):
        if p[i]:
            for j in range(2 * i * (i + 1), m, 2 * i + 1):
                p[j] = False

    a = [2 * i + 1 for i in range(m) if p[i]]
    a[0] = 2
    return a


N = 10 ** 5
P = set(prime_numbers(N))
# これが素数
# print(P)
P = [n for n in P if (n + 1) // 2 in P]
# 自身 + 1 //2 も素数であるもののみに絞る
# print(P)
a = [0] * (N + 1)
for n in P:
    # likenumberは1にする
    a[n] = 1
# 累積和に変換
a = list(accumulate(a))


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

Buggy python submission (verdict: Time Limit Exceeded):
import math
Q = int(input())
num_ranges = [list(map(int, input().split())) for _ in range(Q)]

primes = [2]


def is_prime(num):
    # 今まででた素数で割っていくと、自身が素数かどうかわかる
    if num == 1:
        return False
    if primes[-1] <= math.ceil(math.sqrt(num)):
        for i in range(primes[-1], math.ceil(math.sqrt(num)) + 1):
            is_prime = True
            # 今まででた素数で割っていくと、自身が素数かどうかわかる
            for p in primes:
                if i % p == 0:
                    is_prime = False
            if is_prime:
                primes.append(i)
    for p in primes:
        # 全てprimesを見るのは時間が勿体無いので、自身よりも大きいもの・もしくは同じになったら抜ける.
        # もし素数でないのであれば、p<nの間にあるはず。ない状態で、同じ数値まで来た場合も素数
        if num <= p:
            return True
        if num % p == 0:
            return False
    return True


def check(num):
    if is_prime(num) and is_prime((num + 1) / 2):
        return True
    else:
        return False


for start, end in num_ranges:
    count = 0
    for i in range(start, end + 1):
        if i % 2 == 0:
            continue
        if check(i):
            # print(i)
            count += 1
    print(count)


Fixed submission:
from itertools import accumulate
INF = float('inf')


def prime_numbers(n):
    if n < 2:
        return []

    m = (n + 1) // 2
    p = [True] * m
    for i in range(1, int((n ** 0.5 - 1) / 2) + 1):
        if p[i]:
            for j in range(2 * i * (i + 1), m, 2 * i + 1):
                p[j] = False

    a = [2 * i + 1 for i in range(m) if p[i]]
    a[0] = 2
    return a


N = 10 ** 5
P = set(prime_numbers(N))
# これが素数
# print(P)
P = [n for n in P if (n + 1) // 2 in P]
# 自身 + 1 //2 も素数であるもののみに絞る
# print(P)
a = [0] * (N + 1)
for n in P:
    # likenumberは1にする
    a[n] = 1
# 累積和に変換
a = list(accumulate(a))


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