Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: How many unordered pairs of primes (p <= q) sum to 100? Run the code.

Problem: How many unordered pairs of primes (p <= q) sum to 100? Run the code.
def prime(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, 51) if prime(p) and prime(100 - p)))
What is printed?
  1. A. 12
  2. B. 15
  3. C. 6
  4. D. 25

Correct answer: C. 6

Pairs: 3+97, 11+89, 17+83, 29+71, 41+59, 47+53, so 6. Counting ordered pairs doubles this to 12, while 15 is just the number of primes up to 50 and 25 the number of primes below 100, neither of which checks the partner 100 − p.

More Coding Interview Problems questions

Exams that ask this topic