Data Structures & AlgorithmsHardCodeforces Div2 B · number theory

Problem: How many integers from 1 to 100 have exactly 3 positive divisors?

Problem: How many integers from 1 to 100 have exactly 3 positive divisors?
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. 3
  2. B. 5
  3. C. 6
  4. D. 4

Correct answer: D. 4

A number has exactly three divisors only when it is p^2 for a prime p, giving 4, 9, 25 and 49. Forgetting 49 and counting 1 as a square gives the trap answers.

More Coding Interview Problems questions

Exams that ask this topic