A prime is a positive integer X that has exactly two distinct divisors: 1 and X. The first few prime integers are 2, 3, 5, 7, 11 and 13.
A semiprime is a natural number that is the product of two (not necessarily distinct) prime numbers. The first few semiprimes are 4, 6, 9, 10, 14, 15, 21, 22, 25, 26.
You are given two non-empty arrays P and Q, each consisting of M integers. These arrays represent queries about the number of semiprimes within specified ranges.
Query K requires you to find the number of semiprimes within the range (P[K], Q[K]), where 1 ≤ P[K] ≤ Q[K] ≤ N.
For example, consider an integer N = 26 and arrays P, Q such that:
P[0] = 1 Q[0] = 26 P[1] = 4 Q[1] = 10 P[2] = 16 Q[2] = 20The number of semiprimes within each of these ranges is as follows:
- (1, 26) is 10,
- (4, 10) is 4,
- (16, 20) is 0.
Write a function:
def solution(N, P, Q)
that, given an integer N and two non-empty arrays P and Q consisting of M integers, returns an array consisting of M elements specifying the consecutive answers to all the queries.
For example, given an integer N = 26 and arrays P, Q such that:
P[0] = 1 Q[0] = 26 P[1] = 4 Q[1] = 10 P[2] = 16 Q[2] = 20the function should return the values [10, 4, 0], as explained above.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..50,000];
- M is an integer within the range [1..30,000];
- each element of arrays P and Q is an integer within the range [1..N];
- P[i] ≤ Q[i].
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N, P, Q):
sieve = [True] * (N+1)
sieve[0] = sieve[1] = False
i = 2
while i * i <= N:
if sieve[i]:
k = i * i
while k <= N:
sieve[k] = False
k += i
print(k)
i+= 1
print(sieve)
Invalid result type, array expected, <class 'NoneType'> found.stdout:
6 8 10 12 14 16 18 20 22 24 26 28 12 15 18 21 24 27 30 [False, False, True, True, False, True, False, True, False, False, False, True, False, True, False, False, False, True, False, True, False, False, False, True, False, False, False]
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N, P, Q):
sieve = [T] * (N+1)
sieve[0] = sieve[1] = False
i = 2
while i * i <= N:
if sieve[i]:
k = i * i
while k <= N:
sieve[k] = False
k += i
print(k)
i+= 1
print(sieve)
Invalid result type, array expected, <class 'NoneType'> found.stdout:
6 8 10 12 14 16 18 20 22 24 26 28 12 15 18 21 24 27 20 24 28 30 [-1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N, P, Q):
sieve = [1] * (N+1)
sieve[0] = sieve[1] = -1
i = 2
while i * i <= N:
if sieve[i] == 1:
k = i * i
while k <= N:
sieve[k] = 1
k += i
print(k)
i+= 1
print(sieve)
Invalid result type, array expected, <class 'NoneType'> found.stdout:
6 8 10 12 14 16 18 20 22 24 26 28 12 15 18 21 24 27 20 24 28 30 [-1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
Invalid result type, array expected, <class 'NoneType'> found.stdout:
helo
Invalid result type, array expected, <class 'NoneType'> found.
Invalid result type, array expected, <class 'NoneType'> found.stdout:
[0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0]
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N, P, Q):
sieve = [1 for _ in range(N + 1)]
sieve[0]=sieve[1] = 0
i = 0
while i * i <= N:
if sieve[i]:
k = i * i
while k <= N:
sieve[k] = 0
k += i
i+= 1
prime = []
for i, _ in
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N, P, Q):
sieve = [1 for _ in range(N + 1)]
sieve[0]=sieve[1] = 0
i = 0
while i * i <= N:
if sieve[i]:
k = i * i
while k <= N:
sieve[k] = 0
k += i
i+= 1
prime = []
for i, is_prime in enumerate()
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N, P, Q):
sieve = [1 for _ in range(N + 1)]
sieve[0]=sieve[1] = 0
i = 0
while i * i <= N:
if sieve[i]:
k = i * i
while k <= N:
sieve[k] = 0
k += i
i+= 1
prime = []
for i, is_prime in enumerate(sieve):
if is_prime:
prime.append(i)
print(prime)
Invalid result type, array expected, <class 'NoneType'> found.stdout:
[2, 3, 5, 7, 11, 13, 17, 19, 23]
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N, P, Q):
sieve = [1 for _ in range(N + 1)]
sieve[0]=sieve[1] = 0
i = 0
while i * i <= N:
if sieve[i]:
k = i * i
while k <= N:
sieve[k] = 0
k += i
i+= 1
prime = []
for i, is_prime in enumerate(sieve):
if is_prime:
prime.append(i)
print(prime)
for num in pr
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N, P, Q):
sieve = [1 for _ in range(N + 1)]
sieve[0]=sieve[1] = 0
i = 0
while i * i <= N:
if sieve[i]:
k = i * i
while k <= N:
sieve[k] = 0
k += i
i+= 1
prime = []
for i, is_prime in enumerate(sieve):
if is_prime:
prime.append(i)
print(prime)
semi_prime = []
for num in prime:
for next_num in prime:
semi_prime
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N, P, Q):
sieve = [1 for _ in range(N + 1)]
sieve[0]=sieve[1] = 0
i = 0
while i * i <= N:
if sieve[i]:
k = i * i
while k <= N:
sieve[k] = 0
k += i
i+= 1
prime = []
for i, is_prime in enumerate(sieve):
if is_prime:
prime.append(i)
print(prime)
semi_prime = []
for num in prime:
for next_num in prime:
if num * next_num > N:
break
else:
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N, P, Q):
sieve = [1 for _ in range(N + 1)]
sieve[0]=sieve[1] = 0
i = 0
while i * i <= N:
if sieve[i]:
k = i * i
while k <= N:
sieve[k] = 0
k += i
i+= 1
prime = []
for i, is_prime in enumerate(sieve):
if is_prime:
prime.append(i)
print(prime)
semi_prime = []
for num in prime:
for next_num in prime:
if num * next_num > N:
break
else:
print(num * next_num)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N, P, Q):
sieve = [1 for _ in range(N + 1)]
sieve[0]=sieve[1] = 0
i = 0
while i * i <= N:
if sieve[i]:
k = i * i
while k <= N:
sieve[k] = 0
k += i
i+= 1
prime = []
for i, is_prime in enumerate(sieve):
if is_prime:
prime.append(i)
print(prime)
semi_prime = []
for num in prime:
for next_num in prime:
if num * next_num > N:
break
else:
print(num * next_num)
Invalid result type, array expected, <class 'NoneType'> found.stdout:
[2, 3, 5, 7, 11, 13, 17, 19, 23] 4 6 10 14 22 26 6 9 15 21 10 15 25 14 21 22 26
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N, P, Q):
sieve = [1 for _ in range(N + 1)]
sieve[0]=sieve[1] = 0
i = 0
while i * i <= N:
if sieve[i]:
k = i * i
while k <= N:
sieve[k] = 0
k += i
i+= 1
prime = []
for i, is_prime in enumerate(sieve):
if is_prime:
prime.append(i)
#print(prime)
semi_prime = []
for num in prime:
for next_num in prime:
if num * next_num > N:
break
else:
print(num * next_num)
Invalid result type, array expected, <class 'NoneType'> found.stdout:
4 6 10 14 22 26 6 9 15 21 10 15 25 14 21 22 26
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N, P, Q):
sieve = [1 for _ in range(N + 1)]
sieve[0]=sieve[1] = 0
i = 0
while i * i <= N:
if sieve[i]:
k = i * i
while k <= N:
sieve[k] = 0
k += i
i+= 1
prime = []
for i, is_prime in enumerate(sieve):
if is_prime:
prime.append(i)
#print(prime)
semi_prime = [0 for i in ]
for num in prime:
for next_num in prime:
if num * next_num > N:
break
else:
print(num * next_num)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N, P, Q):
sieve = [1 for _ in range(N + 1)]
sieve[0]=sieve[1] = 0
i = 0
while i * i <= N:
if sieve[i]:
k = i * i
while k <= N:
sieve[k] = 0
k += i
i+= 1
prime = []
for i, is_prime in enumerate(sieve):
if is_prime:
prime.append(i)
#print(prime)
semi_prime = [0 for _ in range(N + 1)]
for num in prime:
for next_num in prime:
if num * next_num > N:
break
else:
print(num * next_num)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N, P, Q):
sieve = [1 for _ in range(N + 1)]
sieve[0]=sieve[1] = 0
i = 0
while i * i <= N:
if sieve[i]:
k = i * i
while k <= N:
sieve[k] = 0
k += i
i+= 1
prime = []
for i, is_prime in enumerate(sieve):
if is_prime:
prime.append(i)
#print(prime)
semi_prime = [0 for _ in range(N + 1)]
for num in prime:
for next_num in prime:
if num * next_num > N:
break
else:
#print(num * next_num)
semi_prime[num * next_num] = 1
print(semi_prime)
Invalid result type, array expected, <class 'NoneType'> found.stdout:
[0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1]
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N, P, Q):
sieve = [1 for _ in range(N + 1)]
sieve[0]=sieve[1] = 0
i = 0
while i * i <= N:
if sieve[i]:
k = i * i
while k <= N:
sieve[k] = 0
k += i
i+= 1
prime = []
for i, is_prime in enumerate(sieve):
if is_prime:
prime.append(i)
#print(prime)
semi_prime = [0 for _ in range(N + 1)]
for num in prime:
for next_num in prime:
if num * next_num > N:
break
else:
#print(num * next_num)
semi_prime[num * next_num] = 1
print(semi_prime)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N, P, Q):
sieve = [1 for _ in range(N + 1)]
sieve[0]=sieve[1] = 0
i = 0
while i * i <= N:
if sieve[i]:
k = i * i
while k <= N:
sieve[k] = 0
k += i
i+= 1
prime = []
for i, is_prime in enumerate(sieve):
if is_prime:
prime.append(i)
#print(prime)
semi_prime = [0 for _ in range(N + 1)]
for num in prime:
for next_num in prime:
if num * next_num > N:
break
else:
#print(num * next_num)
semi_prime[num * next_num] = 1
print(semi_prime)
for (p, q)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N, P, Q):
sieve = [1 for _ in range(N + 1)]
sieve[0]=sieve[1] = 0
i = 0
while i * i <= N:
if sieve[i]:
k = i * i
while k <= N:
sieve[k] = 0
k += i
i+= 1
prime = []
for i, is_prime in enumerate(sieve):
if is_prime:
prime.append(i)
#print(prime)
semi_prime = [0 for _ in range(N + 1)]
for num in prime:
for next_num in prime:
if num * next_num > N:
break
else:
#print(num * next_num)
semi_prime[num * next_num] = 1
print(semi_prime)
for (start, end) in zip(P, Q):
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N, P, Q):
sieve = [1 for _ in range(N + 1)]
sieve[0]=sieve[1] = 0
i = 0
while i * i <= N:
if sieve[i]:
k = i * i
while k <= N:
sieve[k] = 0
k += i
i+= 1
prime = []
for i, is_prime in enumerate(sieve):
if is_prime:
prime.append(i)
#print(prime)
semi_prime = [0 for _ in range(N + 1)]
for num in prime:
for next_num in prime:
if num * next_num > N:
break
else:
#print(num * next_num)
semi_prime[num * next_num] = 1
print(semi_prime)
a
for (start, end) in zip(P, Q):
count = 0
for idx in range(start, end + 1):
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N, P, Q):
sieve = [1 for _ in range(N + 1)]
sieve[0]=sieve[1] = 0
i = 0
while i * i <= N:
if sieve[i]:
k = i * i
while k <= N:
sieve[k] = 0
k += i
i+= 1
prime = []
for i, is_prime in enumerate(sieve):
if is_prime:
prime.append(i)
#print(prime)
semi_prime = [0 for _ in range(N + 1)]
for num in prime:
for next_num in prime:
if num * next_num > N:
break
else:
#print(num * next_num)
semi_prime[num * next_num] = 1
print(semi_prime)
answer = []
for (start, end) in zip(P, Q):
count = 0
for idx in range(start, end + 1):
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N, P, Q):
sieve = [1 for _ in range(N + 1)]
sieve[0]=sieve[1] = 0
i = 0
while i * i <= N:
if sieve[i]:
k = i * i
while k <= N:
sieve[k] = 0
k += i
i+= 1
prime = []
for i, is_prime in enumerate(sieve):
if is_prime:
prime.append(i)
#print(prime)
semi_prime = [0 for _ in range(N + 1)]
for num in prime:
for next_num in prime:
if num * next_num > N:
break
else:
#print(num * next_num)
semi_prime[num * next_num] = 1
print(semi_prime)
answer = []
for (start, end) in zip(P, Q):
count = 0
for idx in range(start, end + 1):
if semi_prime[idx]:
count += 1
answer.append(count)
print(answer)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N, P, Q):
sieve = [1 for _ in range(N + 1)]
sieve[0]=sieve[1] = 0
i = 0
while i * i <= N:
if sieve[i]:
k = i * i
while k <= N:
sieve[k] = 0
k += i
i+= 1
prime = []
for i, is_prime in enumerate(sieve):
if is_prime:
prime.append(i)
#print(prime)
semi_prime = [0 for _ in range(N + 1)]
for num in prime:
for next_num in prime:
if num * next_num > N:
break
else:
#print(num * next_num)
semi_prime[num * next_num] = 1
print(semi_prime)
answer = []
for (start, end) in zip(P, Q):
count = 0
for idx in range(start, end + 1):
if semi_prime[idx]:
count += 1
answer.append(count)
print(answer)
Invalid result type, array expected, <class 'NoneType'> found.stdout:
[0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1] [10, 4, 0]
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N, P, Q):
sieve = [1 for _ in range(N + 1)]
sieve[0]=sieve[1] = 0
i = 0
while i * i <= N:
if sieve[i]:
k = i * i
while k <= N:
sieve[k] = 0
k += i
i+= 1
prime = []
for i, is_prime in enumerate(sieve):
if is_prime:
prime.append(i)
#print(prime)
semi_prime = [0 for _ in range(N + 1)]
for num in prime:
for next_num in prime:
if num * next_num > N:
break
else:
#print(num * next_num)
semi_prime[num * next_num] = 1
#print(semi_prime)
answer = []
for (start, end) in zip(P, Q):
count = 0
for idx in range(start, end + 1):
if semi_prime[idx]:
count += 1
answer.append(count)
print(answer)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N, P, Q):
sieve = [1 for _ in range(N + 1)]
sieve[0]=sieve[1] = 0
i = 0
while i * i <= N:
if sieve[i]:
k = i * i
while k <= N:
sieve[k] = 0
k += i
i+= 1
prime = []
for i, is_prime in enumerate(sieve):
if is_prime:
prime.append(i)
#print(prime)
semi_prime = [0 for _ in range(N + 1)]
for num in prime:
for next_num in prime:
if num * next_num > N:
break
else:
#print(num * next_num)
semi_prime[num * next_num] = 1
#print(semi_prime)
answer = []
for (start, end) in zip(P, Q):
count = 0
for idx in range(start, end + 1):
if semi_prime[idx]:
count += 1
answer.append(count)
#print(answer)
return answer
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N, P, Q):
sieve = [1 for _ in range(N + 1)]
sieve[0]=sieve[1] = 0
i = 0
while i * i <= N:
if sieve[i]:
k = i * i
while k <= N:
sieve[k] = 0
k += i
i+= 1
prime = []
for i, is_prime in enumerate(sieve):
if is_prime:
prime.append(i)
#print(prime)
semi_prime = [0 for _ in range(N + 1)]
for num in prime:
for next_num in prime:
if num * next_num > N:
break
else:
#print(num * next_num)
semi_prime[num * next_num] = 1
#print(semi_prime)
answer = []
for (start, end) in zip(P, Q):
count = 0
for idx in range(start, end + 1):
if semi_prime[idx]:
count += 1
answer.append(count)
#print(answer)
return answer
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N, P, Q):
sieve = [1 for _ in range(N + 1)]
sieve[0]=sieve[1] = 0
i = 0
while i * i <= N:
if sieve[i]:
k = i * i
while k <= N:
sieve[k] = 0
k += i
i+= 1
prime = []
for i, is_prime in enumerate(sieve):
if is_prime:
prime.append(i)
#print(prime)
semi_prime = [0 for _ in range(N + 1)]
for num in prime:
for next_num in prime:
if num * next_num > N:
break
else:
#print(num * next_num)
semi_prime[num * next_num] = 1
#print(semi_prime)
answer = []
for (start, end) in zip(P, Q):
count = 0
for idx in range(start, end + 1):
if semi_prime[idx]:
count += 1
answer.append(count)
#print(answer)
return answer
The following issues have been detected: timeout errors.
large random, length = ~30,000
Killed. Hard limit reached: 6.000 sec.
large random, length = ~30,000
Killed. Hard limit reached: 6.000 sec.