fibonacci sequence....i found this code and it really worked but i don't understand how code was excuted. Thank
I couldn't open your attachments, but there is an example on this page of how to calculate Fibonacci sequences: http://en.wikibooks.org/wiki/Python_Programming/Loops
This is the one i couldn't understand. Can you explain to me the idea please. Thanks
A Fibonnaci sequence is where a number is the sum of the previous two numbers in the sequence (the sequence begins 0, 1). This code gives the first 20 Fibonnaci numbers. So each number is the sum of the previous two. The last three numbers are 1597 2584 4181. 1597 + 2584 = 4181
I can't understand old_a and old_b issues
Those are variables that allow you to keep track of the previous two numbers in the sequence. Try printing out the code and writing in the values as you go along.
sammi789 is a spammer and i would bet her website doesn't specifically cater to the MIT OCW class like this one does.
a = 0 b = 1 count = 0 max_count = 20 while count < max_count: count = count + 1 #we need to keep track of a since we change it old_a = a old_b = b a = old_b b = old_a + old_b #Notice that the , at the end of a print statement keeps it # from switching to a new line print (old_a,) print() so this is the code u worked out right note that Fibonacci number is sum of previous two numbers in the sequence.. ex: if 0,1 are the first two numbers in sequence then the next fibonacci number will be 1(i.e..0+1) so by the above code here two numbers in the sequence are initialized i.e a=0,b=1 here this code prints 20 numbers in fibonacci sequence.. so max_count is initialized as 20. in order to print from beginning a while loop is used. and it is incremented to print next numbers.. old_a,old_b are used to swap these numbers.. U JUST TRY THE COMPUTER EXECUTION ON A PAPER then u can understand how this code is working...
Join our real-time social learning platform and learn together with your friends!