Data Structures & AlgorithmsModerateTCS NQT · counting

Problem: How many integers in [1, 1000] are divisible by both 4 and 9? Run the code.

Problem: How many integers in [1, 1000] are divisible by both 4 and 9? Run the code.
print(sum(1 for x in range(1, 1001) if x % 4 == 0 and x % 9 == 0))
What is printed?
  1. A. 27
  2. B. 250
  3. C. 111
  4. D. 334

Correct answer: A. 27

4 and 9 are coprime, so both means a multiple of 36: 36 · 27 = 972 ≤ 1000, giving 27. 250 counts only multiples of 4, 111 only multiples of 9, and 334 counts 4 or 9 (the union).

More Coding Interview Problems questions

Exams that ask this topic