Data Structures & AlgorithmsModerateTCS NQT · counting

Problem: How many integers in [1, 500] are divisible by 4 or by 6? Run the code.

Problem: How many integers in [1, 500] are divisible by 4 or by 6? Run the code.
print(sum(1 for x in range(1, 501) if x % 4 == 0 or x % 6 == 0))
What is printed?
  1. A. 167
  2. B. 208
  3. C. 125
  4. D. 83

Correct answer: A. 167

Multiples of 4 number 125, of 6 number 83, and of 12 number 41; by inclusion-exclusion 125 + 83 - 41 = 167.

More Coding Interview Problems questions

Exams that ask this topic