ProgrammingHardInfosys · Cognizant

By Fermat's little theorem, for a prime p and a not divisible by p, the inverse of a is a^(p-2) mod p. The procedure below finds the inverse of 3 modulo 11 by fast exponentiation with exponent 9. What is printed?

By Fermat's little theorem, for a prime p and a not divisible by p, the inverse of a is a^(p-2) mod p. The procedure below finds the inverse of 3 modulo 11 by fast exponentiation with exponent 9. What is printed?
result = 1; base = 3; e = 9; m = 11
while e > 0
    if e odd then result = result * base mod m
    base = base * base mod m; e = e div 2
end while
Print result
  1. A. 3
  2. B. 5
  3. C. 4
  4. D. 15

Correct answer: C. 4

Exponent 9 gives 3^9 mod 11 = 4, which is the inverse because 3 x 4 = 12 = 1 mod 11. Printing the base unchanged, using exponent 8, and skipping the mod on the result each give different values.

More Pseudocode and Code Tracing questions

Exams that ask this topic