Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: How many integers in [1, 2000] have an odd number of positive divisors? Run the code.

Problem: How many integers in [1, 2000] have an odd number of positive divisors? Run the code.
print(sum(1 for x in range(1, 2001) if sum(1 for d in range(1, x + 1) if x % d == 0) % 2 == 1))
What is printed?
  1. A. 1000
  2. B. 44
  3. C. 88
  4. D. 1956

Correct answer: B. 44

Only perfect squares have an odd divisor count, and 44² = 1936 ≤ 2000 < 45² = 2025, so 44. Half of 2000 is a guess, 88 double-counts the pairs, and 1956 counts the even-divisor numbers.

More Coding Interview Problems questions

Exams that ask this topic