ProgrammingHardTCS NQT · Cognizant GenC Next

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Function sd(Integer n)
    Integer s = 0
    for (each d from 1 to n - 1)
        if (n mod d == 0)
            s = s + d
        end if
    end for
    return s
End Function
Integer a = 220, b = sd(a)
Print b, sd(b) == a
  1. A. 220 true
  2. B. 284 false
  3. C. 504 false
  4. D. 284 true

Correct answer: D. 284 true

The proper divisors of 220 add to 284, and those of 284 (1 + 2 + 4 + 71 + 142) add back to 220. So 220 and 284 are an amicable pair: 284 true.

More Pseudocode and Code Tracing questions

Exams that ask this topic