Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: How many primes p <= 200 have p + 6 also prime? Run the code.

Problem: How many primes p <= 200 have p + 6 also prime? Run the code.
def isp(n):
    return n > 1 and all(n % d for d in range(2, int(n ** 0.5) + 1))
print(sum(1 for p in range(2, 201) if isp(p) and isp(p + 6)))
What is printed?
  1. A. 26
  2. B. 15
  3. C. 46
  4. D. 39

Correct answer: A. 26

Checking p and p + 6 for primes p ≤ 200 gives 26 pairs. Twin primes (p + 2) give 15, 46 counts all primes ≤ 200, and allowing p − 6 as well gives 39.

More Coding Interview Problems questions

Exams that ask this topic