GATE 2025 DA – Question 63
Consider the following Python code snippet.
def f(a,b):
if (a==0):
return b
if (a%2==1):
return 2*f((a-1)/2,b)
return b+f(a-1,b)
print(f(15,10))The value printed by the code snippet is ______ (*Answer in integer*)
Practise this question in The GATE Grind →
Show answer and explanation
Correct answer: 160
Explanation
Since 15 is odd, $f(15, 10) = 2f(7, 10)$. In the same way $f(7, 10) = 2f(3, 10)$, $f(3, 10) = 2f(1, 10)$ and $f(1, 10) = 2f(0, 10) = 2 \times 10 = 20$. Going back, $f(3, 10) = 40$, $f(7, 10) = 80$ and $f(15, 10) = 160$. The value 160 is printed.