+ 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))

28th Jul 2017, 8:49 PM
Pramesh Sharma
Pramesh Sharma - avatar
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
28th Jul 2017, 9:04 PM
Rrestoring faith
Rrestoring faith - avatar
+ 1
thank you for the clarification
29th Jul 2017, 12:16 AM
Pramesh Sharma
Pramesh Sharma - avatar