ProgrammingHardTCS NQT · Accenture

Let N = 360. The procedure below counts the divisors of N by testing every i from 1 to N. What is printed?

Let N = 360. The procedure below counts the divisors of N by testing every i from 1 to N. What is printed?
count = 0
for i = 1 to N
    if N mod i == 0 then count = count + 1
end for
Print count
  1. A. 23
  2. B. 12
  3. C. 24
  4. D. 1170

Correct answer: C. 24

360 = 2^3 * 3^2 * 5 has (3+1)(2+1)(1+1) = 24 divisors. Excluding N itself gives 23, testing only up to the square root counts just half the pairs, and summing the divisors gives 1170.

More Pseudocode and Code Tracing questions

Exams that ask this topic