ProgrammingHardGoldman Sachs OA · TCS Digital

What will be the output of the following pseudocode? (All variables are integers, so / is integer division)

What will be the output of the following pseudocode? (All variables are integers, so / is integer division)
Function phi(Integer n)
    Integer r = n, p = 2
    while (p * p <= n)
        if (n mod p == 0)
            while (n mod p == 0)
                n = n / p
            end while
            r = r - r / p
        end if
        p = p + 1
    end while
    if (n > 1)
        r = r - r / n
    end if
    return r
End Function
Print phi(36), phi(97)
  1. A. 12 96
  2. B. 24 96
  3. C. 12 97
  4. D. 18 96

Correct answer: A. 12 96

This is Euler's totient: 36 = 2² × 3², so φ(36) = 36 × (1/2) × (2/3) = 12; 97 is prime, so φ(97) = 96. Output 12 96.

More Pseudocode and Code Tracing questions

Exams that ask this topic