Data Structures & AlgorithmsHardCAT · ICPC regional

Problem: How many pairs (a, b) with 1 ≤ a < b ≤ 20 satisfy gcd(a, b) = 1?

  1. A. 128
  2. B. 127
  3. C. 254
  4. D. 119

Correct answer: B. 127

For each b the coprime a < b number φ(b), so the count is φ(2) + … + φ(20) = 128 − φ(1) = 127. Including the pair (1, 1) gives 128, counting ordered pairs gives 254, and stopping at 19 drops φ(20) = 8 (119).

More Coding Interview Problems questions

Exams that ask this topic