Data Structures & AlgorithmsModerateAmazon · Microsoft OA

Problem: Count the integers in [1, 1000] that are coprime to 1000.

Problem: Count the integers in [1, 1000] that are coprime to 1000.
from math import gcd
print(sum(1 for i in range(1, 1001) if gcd(i, 1000) == 1))
What is printed?
  1. A. 500
  2. B. 100
  3. C. 400
  4. D. 600

Correct answer: C. 400

1000 = 2³ · 5³, so φ(1000) = 1000 × (1 − 1/2) × (1 − 1/5) = 400. Removing only the even numbers (500) forgets the odd multiples of 5.

More Coding Interview Problems questions

Exams that ask this topic