Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: How many numbers in [1, 500] are not divisible by 2, 3 or 5? Run the code.

Problem: How many numbers in [1, 500] are not divisible by 2, 3 or 5? Run the code.
print(sum(1 for x in range(1, 501) if x % 2 and x % 3 and x % 5))
What is printed?
  1. A. 484
  2. B. 133
  3. C. 366
  4. D. 134

Correct answer: D. 134

Inclusion-exclusion gives 500 − 516 + 166 − 16 = 134 numbers coprime to 30. Using or instead of and gives 484, the density estimate 500 · 8/30 gives 133, and 366 counts the complement.

More Coding Interview Problems questions

Exams that ask this topic