+ 2
Factorial
hi guys, this is the famous factorial function def factorial(x): if x == 1: return 1 else: return x * factorial(x-1) print(factorial(5)) but i dont understend this. could anybody describe this process in details? what exactly happend with variable? if return() return new meaning why i dont have 5*4*20*19....
7 ответов
+ 8
def factorial(x):
if x == 1:
return 1
else:
return x * factorial(x-1)
print(factorial(5))
when value of x is 1 then return 1 else return x * factorial(x-1)
so x=5 if condition false evaluate else part
5!=1 so
5* factorial(5-1)=5*factorial(4)
now value of x is 4 so if condition false evaluate else part
4!=1 so 5*4*factorial(3)
then value of x=3 so if condition false evaluate else part
3!=1 so 5*4*3* factorial(2)
value of x=2 so if condition false evaluate else part
2!=1 so 5*4*3*2* factorial(1)
value of x is 1 so it will return 1 because if condition true so evaluate if part and returns 1
so finally the answer will be
5*4*3*2*1=120
+ 5
yeah it's example of recursive function where a function calls itself again and again till the condition gets true
+ 5
1: def factorial(x):
2: if x == 1:
3: return 1
4: else:
5: return x * factorial(x-1)
6:
7: print(factorial(5))
Start executing line 7 by calling factorial with 5.
Enter factorial line 1 with x of 5.
Test condition on line 2 5 isn't 1 so skip to line 5.
Start line 5 by calling factorial again passing 5-1 or 4.
Enter factorial line 1 with x of 4.
Test condition on line 2 4 isn't 1 so skip to line 5.
Start line 5 by calling factorial again passing 4-1 or 3.
Enter factorial line 1 with x of 3.
Test condition on line 2 3 isn't 1 so skip to line 5.
Start line 5 by calling factorial again passing 3-1 or 2.
Enter factorial line 1 with x of 2.
Test condition on line 2 2 isn't 1 so skip to line 5.
Start line 5 by calling factorial again passing 2-1 or 1.
Enter factorial line 1 with x of 1.
Test condition on line 2 1 is 1 so skip to line 3.
Return 1 to caller.
Back to line 5 with x of 2 and factorial return of 1.
Return 2*1 or 2 to caller.
Back to line 5 with x of 3 and factorial return of 2.
Return 3*2 or 6 to caller.
Back to line 5 with x of 4 and factorial return of 6.
Return 4*6 or 24 to caller.
Back to line 5 with x of 5 and factorial return of 24.
Return 5*24 or 120 to caller.
Back to line 7 with factorial return of 120.
Print the 120.
+ 3
well when using recursion you have to work with the mindset of "I already solved the problem". Recursion can be explained from the end-start and not from the start-end as were used to.
With factorial recursion the pattern is this:
factorial(5)
x=5 -> return 5*factorial(4)= factorial(5)
x=4-> return 4*factorial(3)= factorial(4)
x=3 -> return 3*factorial(2) = factorial(3)
x=2-> return 2*factorial(1) = factorial(2)
x=1 -> return 1 = factorial(1)
Then afterwards the whole process is calculated.
x=2- > return 2*factorial(1)=2
x=3 -> return 3*factorial(2)= 6
x=4 -> return 4*factorial(3) = 24
x=5 -> return 5*factorial(4) = 120
the functions then stops because the max variable which factorial was given to is 5.
+ 1
this is recursive function to calculate factorial of number .
recursive function means function can call itself .
factorial(5) --> assigns x=5
5*factorial(5-1) i.e. factorial(4)
5*4*factorial(4-1) i.e factorial(3)
5*4*3*factorial(3-1) i.e factorial(2)
5*4*3*2*factorial(2-1) i.e factorial(1)
5*4*3*2*1= 120
0
oh, it's like function repeat itself with new meanings?
0
got it