Can someone please explain this code to me? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can someone please explain this code to me?

If num = 8 how would the process go? num = int(input()) def fibonacci(n): if n <= 1: return n else: return fibonacci(n-1) + fibonacci(n-2) for i in range(num): print(fibonacci(i))

2nd May 2022, 5:05 PM
Ava Alford
13 Answers
+ 7
Ava Alford For ex: n=5. fib(5) =>fib(5-2) +fib(5-1) => fib(3) + fib(4) 👇 👇 =>[ fib(3-2) + fib(3-1) ] + [ fib(4-2)+fib(4-1) ] = >[ fib(1) + fib(2) ] + [ fib(2) +fib(3) ] =>1 + fib(2-2)+ fib(2-1) +fib(2-2)+fib(2-1) +[ fib(1) + fib(2)] =>1+fib(0)+fib(1)+ fib(0)+fib(1) + [ 1 + fib(2-2)+ fib(2-1)] =>1+1+1+1+1+ [ 1+ fib(0)+ fib(1)] =>5+ [ 1+1+1] => 5+3 = 8. Here 5th number is 8 . Also you can see that 4th number is 5 and 3th number is 3 This is the way it been evaluted by code.. I had posted this in link earlier, https://www.sololearn.com/discuss/2125616/?ref=app Links have some other example, may help to understand I think. Hope it helps..
2nd May 2022, 7:06 PM
Jayakrishna 🇮🇳
+ 3
If do it on paper you start with 1,1 1+1 = 2 1,1,2 1+2 = 3 1,1,2,3 2+3=5 1,1,2,3,5 3+5=8 1,1,2,3,5,8 And so on... Hope this helps you.
2nd May 2022, 6:15 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
Ava Alford num = 8 fibonnaci(n) = F(n) F(8) / \ F(7) F(6) / \ / \ F(6) F(5) F(5) F(4) You can continue this tree until you get F(1) and F(0) on each branch. F(0) returns 0 and F(1) returns 1 F(0) + F(1) = F(2) = 1 F(1) + F(2) = F(3) = 2 F(2) + F(3) = F(4) = 3 F(3) + F(4) = F(5) = 5 F(4) + F(5) = F(6) = 8 F(5) + F(6) = F(7) = 13 F(6) + F(7) = F(8) = 21 If n = 8 then you get 21 because 21 is the 8th fibonacci number. n 0 1 2 3 4 5 6 7 8.... f 0 1 1 2 3 5 8 13 21....
2nd May 2022, 6:41 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
2nd May 2022, 5:38 PM
Ava Alford
+ 1
Ava Alford The other way around would be to say okay I want fibonacci(8) so let's start at 1,1 until I have 8 fibonacci numbers. You would do this with a normal loop. But here you say I want fibonacci(8), I don't know fibonacci(7) and fibonacci(6) but I know that f(8) is the sum of f(7) and f(6) (that's how fibonacci works f(n) = f(n-2) + f(n-1)) So each function call needs another function call until fibonacci(0) and fibonacci(1) are called. They return their values to the other function calls until fibonacci(8) get its value. And this is how recursion works.
2nd May 2022, 6:54 PM
Denise Roßberg
Denise Roßberg - avatar
0
Jayakrishna🇮🇳 so I almost understand it. Can you please run me through how the code would work if num = 8? I'm having issues with that.
2nd May 2022, 5:32 PM
Ava Alford
0
https://www.sololearn.com/discuss/2125616/?ref=app https://www.sololearn.com/discuss/2222560/?ref=app https://www.sololearn.com/discuss/2389918/?ref=app Ava Alford hope these helps.. go through ones.. I have already added explaination in those for some samples.. with step by steps.. hope it helps to understand it..
2nd May 2022, 5:35 PM
Jayakrishna 🇮🇳
0
For 8: 1, 1, 2, 3, 5, 8, 13, 21 21 you get. Ava Alford You're welcome..
2nd May 2022, 5:44 PM
Jayakrishna 🇮🇳
0
Jayakrishna🇮🇳 I mean how do you 8 from 3 and 5 with the code? When I work through it on paper I always get 7 which I know is incorrect
2nd May 2022, 6:11 PM
Ava Alford
0
How 7 ? 3+5 = 8 Is not it?
2nd May 2022, 6:13 PM
Jayakrishna 🇮🇳
0
I understand the math behind it. I'm trying to work through how the code does that math
2nd May 2022, 6:23 PM
Ava Alford
0
Fibonacci Sequence starts with n = 0, 1, 2, 3, 4, 5, 6, 7, 8,... this then gives: 0, 1, 1, 2, 3, 5, 8, 13, 21,... That is at: n = 0, it has 0 n = 1, it has 1 n = 2, it has 1 n = 3, it has 2 n = 4, it has 3 n = 5, it has 5 n = 6, it has 8 This shows that from n = 3 upward, the current no is the sum of the two preceding nos. Also, setting the initial condition is also important as the sequence starts from n = 0. Hence the initial conditions of If n = 0: return 0; if n = 1: return 1; and if n = 2: return 1; ought to be set as displayed below: n = int(input("Enter the n value: ")); def fib(n): if n == 0: return 0; elif n == 1 or n == 2: return 1; else: y = fib(n-1) + fib(n-2); return y; print(fib(n));
3rd May 2022, 3:46 PM
Sobola Gabriel
Sobola Gabriel - avatar
- 1
Why your expectation is 0110 ? Can you explain your understandings..!! Fibonachi series is " curent value is sum of precious two terms... 1 1 2 3... 1 1 are initial values and 2 is 1+1 3 is 2+1 .. Sum of privious 2 terms... Hope it helps...
2nd May 2022, 5:12 PM
Jayakrishna 🇮🇳