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- A. 227760
- B. 3
- C. 5
- 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.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Let phi(n) be the number of integers i with 1 <= i <= n that share no common factor with n except 1. The procedure below computes phi(36)…
- Starting from n = 27, the Collatz step is applied: if n is even, n becomes n/2, otherwise n becomes 3n + 1. The procedure below counts the…
- Four numbers 4, 6, 10 and 15 are combined pairwise: the lcm of a and b is computed as ab div gcd(a, b), and the lcm of three numbers is…
- Count the integers n with 1 <= n <= 100 that are divisible by 3 or by 5, but NOT by both. The procedure below does this with a single…
- For N = 100 the procedure below counts how many times 5 divides the factorials, that is, the number of trailing zeros of 100!. The count…
- The procedure below reverses the digits of N = 120450 (trailing zeros disappear in the numeric result). What is printed? rev = 0; m = N…