Data Structures & AlgorithmsModerateTCS NQT · number theory

Problem: How many integers in [1, 999] are divisible by 7 but not by 14? Run the code.

Problem: How many integers in [1, 999] are divisible by 7 but not by 14? Run the code.
print(sum(1 for x in range(1, 1000) if x % 7 == 0 and x % 14 != 0))
What is printed?
  1. A. 71
  2. B. 142
  3. C. 72
  4. D. 70

Correct answer: A. 71

There are 142 multiples of 7 and 71 multiples of 14 below 1000, so 142 - 71 = 71 remain. Including the multiples of 14 gives the trap 142.

More Coding Interview Problems questions

Exams that ask this topic