0
Can anyone explain how this program works ?
def f(n): if n==0: return 0 else: return f(n-1)+10 print(f(5))
6 Respostas
+ 4
Shreepriya HA this is a recursive just like in my c program only it is in python.
https://code.sololearn.com/c8a9dj4m4WgB/?ref=app
https://code.sololearn.com/cUVfXJvJXUl1/?ref=app
+ 3
This is an inductive definition of a function f,
f(n) = f(n-1)+10
with base case, f(0)=0;
In order to understand what this code does, let's see how this gets executed
>>f(5) = f(4)+10
>>f(5) = (f(3)+10)+10
{because f(4)= f(3)+10}
Similarly,
f(5) = (f(2) +10) +10 + 10
f(5) = (f(1) +10) +10 + 10 + 10
f(5) = (f(0) +10) +10 + 10 + 10 +10
So, f(5) = 50
This code will output 50
+ 3
Thank you