Data Structures & AlgorithmsHardGATE CS

How many primes p < 500 have p - 2 or p + 2 also prime (that is, belong to at least one twin-prime pair, counted as individual primes)?

  1. A. 47
  2. B. 24
  3. C. 49
  4. D. 45

Correct answer: A. 47

There are 24 twin-prime pairs below 500. The primes involved are counted once each; only one pair-sharing prime exists (5 lies in both (3,5) and (5,7)), so the count is 47. Doubling the pair count over-counts the shared prime.

More Coding Interview Problems questions

Exams that ask this topic