+ 1

Fibonacci Sequence While Loop

In the following sequence, each number (except the first two) is the sum of the previous two numbers: 0, 1, 1, 2, 3, 5, 8, 13, .... This sequence is known as the Fibonacci sequence. We speak of the i'th element of the sequence (starting at 0)-- thus the 0th element is 0, the 1st element is 1, the 2nd element is 1, the 3rd element is 2 and so on. Given the positive integer n, associate the nth value of the fibonacci sequence with the variable result. For example, if n is associated with the value 8 then result would be associated with 21. answer is:if n <= 1 : result = n else : count = 2 i = 1 lastI = 0 while count <= n : lastI, i = i, lastI + i count += 1 result = i So I am starting to grasp this but there are several points that I do not understand. If someone could possibly break this down It would be appreciated.

17th Apr 2019, 9:07 PM
Justin
5 Answers
+ 3
Hello Justin, I'm a beginner too, but here's my two cents : -- I think "count" refers to the current term in the sequence, starting with 2 because 0 and 1 have already been dealt with in the "if" statement (no calculations involved here : you can't sum the two previous terms of the first two terms..., they don't even exist..., so term 0 = just 0 and term 1 = just 1). -- Then, "i" would be the value of the current term. In the example sequence that you wrote, you can see that i at count 0 = 0, i at count 1 = 1, and i at count 2 = 1 too (sum of the two previous ones). Following the same pattern : i at count 3 = 2 (1 + 1), i at count 4 = 3 (1 + 2), i at count 5 = 5 (2 + 3), i at count 6 = 8 (3 + 5), i at count 7 = 13 (5 + 8), and i at count 8 = 21 (8 + 13) (like you said). -- Quite obviously, "lastI" would be the value of the last "i" that you calculated. For example, at count 8 : i = 21 and lastI = 13 (= i at count 7).
18th Apr 2019, 1:16 AM
Jake Chambers
Jake Chambers - avatar
+ 2
Now let's take a closer look at the line stating that "lastI, i = i, lastI + i". You should consider it as a double assignment, meaning that the value of i (1st item on the right) is passed to lastI (1st item on the left), AND that the value of lastI + i (2nd item on the right) is passed to i (2nd item on the left) simultaneously. This operation is renewed for each term (= each loop) until count = n, and then the function returns i at n = the value of the nth term.
18th Apr 2019, 1:17 AM
Jake Chambers
Jake Chambers - avatar
+ 2
You're welcome Justin, I appreciate the feedback :) Again, I'm a beginner too, so my explanation might not be 100% accurate. I just tried to explain to you the general idea, in a way that I wish it would have been explained to me when I struggled to try and "grasp" it too. I see that my 3rd post has been downvoted. Let alone the fact that I used // to comment (I'm currently studying Java, in Python it should be # if I remember well), I guess it has to do with me erroneously suggesting that the "if" statement is executed prior to the loop. Actually it is not, that was inaccurate. To put it right : -- if you set n = 0 or n = 1, program will execute the "if" statement only (no loop) ; -- if you set n > 1, it's the "else" statement that will be executed, with the loop starting at term 2 (i = value of term 1 = 1, lastI = value of term 0 = 0), and repeating until term n. Hope it's better this way. If someone wants to add, specify and/or correct anything, please do it, you're welcome :) PS. 3rd post edited *
18th Apr 2019, 11:23 AM
Jake Chambers
Jake Chambers - avatar
+ 1
Again, let's assume that n = 8, and see how the code runs : 1) since n > 1, program skips the "if" statement ; 2) instead, it runs the "else" statement, with the loop starting at term 2 (i = value of term 1 = 1, lastI = value of term 0 = 0), and repeating until term n ; 3) the loop runs as follows : -- at term n = 2, lastI = 0 and i = 1, lastl becomes 1 and i becomes 1 ; -- at term n = 3, lastl = 1 and i = 1, lastl becomes 1 and i becomes 2 ; -- at term n = 4, lastl = 1 and i = 2, lastl becomes 2 and i becomes 3 ; -- at term n = 5, lastI = 2 and i = 3, lastI becomes 3 and i becomes 5 ; -- at term n = 6, lastI = 3 and i = 5, lastl becomes 5 and i becomes 8 ; -- at term n = 7, lastl = 5 and i = 8, lastl becomes 8 and i becomes 13 ; -- at term n = 8, lastI = 8 and i = 13, lastI becomes 13 and i becomes 21 ; ... So the 8th term of the Fibonacci sequence is indeed = 21 :) I hope this helps, good luck !
18th Apr 2019, 1:19 AM
Jake Chambers
Jake Chambers - avatar
+ 1
Thanks so much!! I appreciate you taking the time to break it down.
18th Apr 2019, 8:36 AM
Justin