Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice

Problem: How many integers from 1 to 1000 are not divisible by any of 2, 3, 5 and 7? Run the code.

Problem: How many integers from 1 to 1000 are not divisible by any of 2, 3, 5 and 7? Run the code.
print(sum(1 for x in range(1, 1001) if x % 2 and x % 3 and x % 5 and x % 7))
What is printed?
  1. A. 229
  2. B. 228
  3. C. 266
  4. D. 772

Correct answer: B. 228

48 of every 210 numbers are coprime to 210; direct counting up to 1000 gives 228. Rounding 1000 · 48/210 up gives 229, ignoring 7 gives 266, and 772 is the complement.

More Coding Interview Problems questions

Exams that ask this topic