ProgrammingHardCAT · MBA placement

The procedure below sums the prime factors of N = 360 counted with multiplicity (2 counted three times, and so on) by trial division. What is printed?

The procedure below sums the prime factors of N = 360 counted with multiplicity (2 counted three times, and so on) by trial division. What is printed?
s = 0; m = 360; d = 2
while m > 1
    if m mod d == 0 then s = s + d; m = m div d
    else d = d + 1
end while
Print s
  1. A. 10
  2. B. 17
  3. C. 8
  4. D. 360

Correct answer: B. 17

360 = 2^3 * 3^2 * 5, so counted with multiplicity the prime factors sum to 2+2+2+3+3+5 = 17. Counting each prime once gives 10, halving the total gives 8, and multiplying the factors gives 360.

More Pseudocode and Code Tracing questions

Exams that ask this topic