Can anyone explain me how this code works | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
26th May 2019, 2:17 PM
D A Mahesh
D A Mahesh - avatar
5 Answers
+ 1
Airree is_even always hits to zero and is_odd always returns False. the result is determined by the huge amount of not's that accumulate during the recursion. eg not not not not not... False=True/False edit: in general, if there are odd number of not's the number is evaluated to be even ie is_even returns True. for even number of not's the number is even.
26th May 2019, 2:41 PM
Farry
Farry - avatar
+ 2
Let's say it is factorial(3) it returns 3 * factorial(3 - 2) so 3 * 2 * factorial (2 - 1), and when n equals 1, it just returns 1 -> 3 * 2 * 1 == 6, so it just returns 1 * 2 * ... * n
26th May 2019, 2:27 PM
Airree
Airree - avatar
+ 2
Okay, so if a number is bigger than 0, for example 3, then first it returns is_odd(3 - 1), which returns not is_even(2 - 1), and that goes down to 0. So if it hits 0 when it runs with the is_odd, it should return true, but it's not True, so false, if it normally hits 0 (with is_even) it returns true
26th May 2019, 2:35 PM
Airree
Airree - avatar
+ 1
let x=3, then the code goes like this: 3*fac(2) =3*2*fac(1) =3*2*1 the f() returns 1 for x=1 and ends the recursion there. try to grasp on this example for a moment.
26th May 2019, 2:27 PM
Farry
Farry - avatar
0
I want explanation for the code in 3rd page i.e. In the last page of recursion chapter. For the is even and is odd code.
26th May 2019, 2:29 PM
D A Mahesh
D A Mahesh - avatar