ProgrammingHardTCS NQT · Accenture
For the function gcd(a, b) written by the procedure below, what is printed for gcd(252, 105)? (The procedure uses repeated remainder steps.)
For the function gcd(a, b) written by the procedure below, what is printed for gcd(252, 105)? (The procedure uses repeated remainder steps.)
function gcd(a, b)
while b != 0
r = a mod b; a = b; b = r
end while
return a
end function
Print gcd(252, 105)- A. 21
- B. 0
- C. 42
- D. 147
Correct answer: A. 21
The remainders are 252 mod 105 = 42, 105 mod 42 = 21, and 42 mod 21 = 0, so the gcd is 21. Returning b gives 0, stopping after one remainder step gives 42, and printing the first subtraction 252 - 105 gives 147.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- The Fibonacci numbers F(0) = 0, F(1) = 1, F(n) = F(n-1) + F(n-2) satisfy gcd(F(m), F(n)) = F(gcd(m, n)). The procedure below uses this…
- The procedure below computes the last digit of 7^100 by starting from d = 1 and multiplying by 7, keeping only the last digit, once for…
- The number of ways to write 20 as p + q with p and q prime and p <= q is counted by the procedure below, which tries p = 2 to 10 and sets…
- For n = 1 to 100, the procedure below counts how many n satisfy that nn ends with the digit 1. What is printed? count = 0 for n = 1 to 100…
- The array a = [2, 3, 4, 1, 2, 3, 9] is scanned for its longest run of consecutive positions where each element is exactly one more than…
- The stack-based procedure below computes, for each index i of a = [4, 2, 6, 1, 5], the index of the next greater element to its right (or…