Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice
Problem: How many twin prime pairs (p, p + 2), both prime, have p + 2 below 10000? Run the sieve.
Problem: How many twin prime pairs (p, p + 2), both prime, have p + 2 below 10000? Run the sieve.
N = 10000
s = [True] * (N + 1)
s[0] = s[1] = False
for i in range(2, 101):
if s[i]:
for j in range(i * i, N + 1, i):
s[j] = False
print(sum(1 for k in range(2, N - 1) if s[k] and s[k + 2]))What is printed?- A. 206
- B. 205
- C. 204
- D. 210
Correct answer: B. 205
A sieve up to 10000 followed by a scan for p and p + 2 both prime gives 205 twin pairs.
More Coding Interview Problems questions
- Problem: How many unordered pairs of primes (p <= q) sum to 100? Run the code. python def prime(n): return n > 1 and all(n % d for d in…
- Problem: How many integer lattice points (x, y) lie on the circle x^2 + y^2 = 25? Run the code. python print(sum(1 for x in range(-5, 6)…
- Problem: How many integers in [1, 10000] have decimal digits summing to 12? Run the code. python print(sum(1 for x in range(1, 10001) if…
- Problem: What is the length of the longest palindromic subsequence of 'character'? Run the DP. python def lps(s): n = len(s) dp = [[0] n…
- Problem: What is the minimum number of characters to insert to make 'leetcode' a palindrome? Use length minus longest palindromic…
- Problem: How many binary strings of length 6 contain no three consecutive 1s? Run the code. python import itertools print(sum(1 for t in…