+ 1
Need help: my code is recursion or not?
Could you explain why my code is not recursion ? https://code.sololearn.com/cdBCJB7mBx6J/?ref=app
6 Respuestas
+ 5
Recursion is when something is defined in terms of itself, for instance to calculate a factorial or a fibonacci number. In those algorithms you start from a base case, and then you use the result to build your new result for the next n.
For exampel for the factoral n! = 1*2*3*…*n, you have:
• base case: n = 0 gives F(n) = 1
• otherwise: n > 0 gives F(n) = n * F(n-1)
So we have that:
F(3) =
= 3 * F(2) =
= 3 * (2 * F(1)) =
= 3 * (2 * (1 * F(0))) =
= 3 * (2 * (1 * (1))) =
= 3 * (2 * (1)) =
= 3 * (2) =
= 6
For the Factorial, you can express this in Python in the following way:
https://code.sololearn.com/cirJOt2W2zTb/?ref=app
For the Fibonacci series, you can express this in Python in the following way:
https://code.sololearn.com/cWNQ2Qk48OMq/?ref=app
+ 3
Ksndr well it looks right, but it'll be better to ask suggestions of someone very good at Python. Rellot's screwdriver can you answer this buddy?
+ 2
Yeah, your code is using recursion. Your function calls itself based on a condition, and has a termination condition. So I think it sure is recursion
+ 2
Rishi do I look like I know python?
I've never even read functions of python!
afaik my C++ knowledge goes... theoretically it should work but the output doesn't seem so
off topic here:
"white cats with grey color in head"
Rishi that cat(maybe a kitten since he's just 2 year old) is also known Norwegian Forest Cat
+ 2
Rellot's screwdriver oh I thought white cats with grey color in head can do Python good😗
+ 1
It is my first recursion. Im not sure it right. I think I made the function call cheating - I used it to create a loop. is it correct to do so?