Data Structures & AlgorithmsModerateProduct Company SDE · CP practice

Problem: How many integers in [1, 100] are divisible by neither 2 nor 3? Run the code.

Problem: How many integers in [1, 100] are divisible by neither 2 nor 3? Run the code.
print(sum(1 for x in range(1, 101) if x % 2 != 0 and x % 3 != 0))
What is printed?
  1. A. 17
  2. B. 50
  3. C. 67
  4. D. 33

Correct answer: D. 33

Inclusion-exclusion: 100 − 50 − 33 + 16 = 33. Forgetting to add back the 16 multiples of 6 gives 17, removing only the evens gives 50, and 67 counts the numbers divisible by 2 or 3 instead.

More Coding Interview Problems questions

Exams that ask this topic