GATE DA 2025 · Programming in Python

NAT · 2 marks · Problem PROG-PYBS-0006

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)

Numerical answer (NAT).

Solve it on GATE DA practice →More Python Programming problems

Show the worked solution\(f(a,b)\): if \(a=0\), return \(b\); if \(a\) odd, return \(2f((a-1)/2,b)\); else return \(b+f(a-1,b)\). Since \(15,7,3,1\) are all odd:\(f(15,10)=2f(7,10)=2\big[2f(3,10)\big]=2\big[2[2f(1,10)]\big]=2\big[2[2[2f(0,10)]]\big]=2\cdot2\cdot2\cdot2\cdot f(0,10)=16\times10=160\)Answer: 160Click Here for Detail SolutionGateOverflow discussion: https://gateoverflow.in/460969/gate-da-2025-question-53

GATE DA practice: past exam problems by chapter, with worked solutions · gatedaprep.com