Data Structures & AlgorithmsHardCodeforces Div2 C · divisor counting

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

Problem: How many integers in [1, 100] have exactly 4 positive 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) == 4))
What is printed?
  1. A. 24
  2. B. 32
  3. C. 28
  4. D. 30

Correct answer: B. 32

Numbers with exactly 4 divisors are p^3 or p*q for distinct primes; in [1, 100] these are 8, 27 and 30 semiprimes, 32 in total.

More Coding Interview Problems questions

Exams that ask this topic