4 Respostas
+ 7
I think the explanation here is quite okay:
https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2465/
Basically, for 5 it would go like:
f(5) = 5 * f(5-1)
f(5-1) = (5-1) * f(5-1-1)
f(5-1-1) = (5-1-1) * f(5-1-1-1)
f(5-1-1-1) = (5-1-1-1) * f(5-1-1-1-1)
f(5-1-1-1-1) = (5-1-1-1-1) * f(5-1-1-1-1-1)
f(5-1-1-1-1-1) = f(0), so here we return 1 -- it's the so-called base case.
Now, if you replace all f(x) with relevant input, you get 5*4*3*2*1*1 = 120, which is what f(5) is equal to.
0
there is a useful link for a youtube video there by the comments, have a look there.
0
the last line is the actual recursion.
keep calling the function factorial(n), reducing n by 1 in each call until n == 0 then stop and return 1
0
thanks!