ProgrammingHardInfosys · Cognizant

Compute 3^13 mod 7 with the binary method below (a = 3, e = 13, m = 7). What is printed?

Compute 3^13 mod 7 with the binary method below (a = 3, e = 13, m = 7). What is printed?
result = 1; base = a mod m; e = 13
while e > 0
    if e is odd then result = (result * base) mod m
    base = (base * base) mod m
    e = e div 2
end while
Print result
  1. A. 227760
  2. B. 3
  3. C. 5
  4. D. 10

Correct answer: B. 3

3^13 = 1594323 and 1594323 mod 7 = 3, which the binary method reaches by squaring the base mod 7 and multiplying in the odd bits. Printing the integer quotient 3^13 div 7 gives 227760, dividing the exponent by three gives 5, and adding m to the answer gives 10.

More Pseudocode and Code Tracing questions

Exams that ask this topic