ProgrammingVery hardCAT · MBA placement
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 identity to find gcd(F(12), F(18)): it computes g = gcd(12, 18) and then prints F(g). What is printed?
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 identity to find gcd(F(12), F(18)): it computes g = gcd(12, 18) and then prints F(g). What is printed?
g = gcd(12, 18) compute F(g) by iteration from F(0) = 0, F(1) = 1 Print F(g)
- A. 5
- B. 8
- C. 13
- D. 30
Correct answer: B. 8
gcd(12,18) = 6 and F(6) = 8. Computing F(5) gives 5, F(7) gives 13, and adding the two indices gives 30.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- 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…
- An array a = [1, 3, 5, 7, 9, 11, 13, 15] is searched with binary search for the target 15, and the procedure counts how many array…