Data Structures & AlgorithmsModerateProduct Company SDE · CP practice

Problem: How many integers in [1, 10000] are divisible by 9 but not by 27? Run the code.

Problem: How many integers in [1, 10000] are divisible by 9 but not by 27? Run the code.
print(sum(1 for x in range(1, 10001) if x % 9 == 0 and x % 27 != 0))
What is printed?
  1. A. 1111
  2. B. 741
  3. C. 370
  4. D. 556

Correct answer: B. 741

Multiples of 9: 1111; of 27: 370; difference 741. 1111 forgets to exclude 27, 370 is the excluded set, and subtracting multiples of 18 gives 556.

More Coding Interview Problems questions

Exams that ask this topic