Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: How many integers in [1, 100] have exactly 3 divisors? Run the code.

Problem: How many integers in [1, 100] have exactly 3 divisors? Run the code.
print(sum(1 for x in range(1, 101) if sum(1 for d in range(1, x + 1) if x % d == 0) == 3))
What is printed?
  1. A. 4
  2. B. 5
  3. C. 3
  4. D. 6

Correct answer: A. 4

Only squares of primes have exactly three divisors, and the primes up to 10 give 4, 9, 25 and 49.

More Coding Interview Problems questions

Exams that ask this topic