Data Structures & AlgorithmsModerateICPC regional · number theory warm-up

Problem: How many ordered pairs (a, b) with 1 <= a, b <= 6 have gcd(a, b) = 1?

Problem: How many ordered pairs (a, b) with 1 <= a, b <= 6 have gcd(a, b) = 1?
from math import gcd
print(sum(1 for a in range(1, 7) for b in range(1, 7) if gcd(a, b) == 1))
What is printed?
  1. A. 21
  2. B. 25
  3. C. 23
  4. D. 18

Correct answer: C. 23

Pairs with gcd above 1 number 13: those with both entries even (9 pairs) plus pairs with a common factor 3 or 5 not already counted. The remaining 23 pairs are coprime.

More Coding Interview Problems questions

Exams that ask this topic