+ 1
Can someone explain to me how the answer to the code below is 300.
def f(n): if n == 0: return 0 else: return f(n-1) + 100 print(f(3))
2 Respuestas
+ 4
f(3) = f(3-1) + 100 = ?
But what's f(3-1)?
f(3-1) = f(2-1) + 100
But what's f(2-1)?
f(2-1) = f(1-1) + 100
But what's f(1-1)?
f(0) = 0.
Therefore,
f(2-1) = f(1-1) + 100 = 100
f(3-1) = f(2-1) + 100 = 200
f(3) = f(3-1) + 100 = 300
Output: 300
+ 1
thank you for the clarification