Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 18 Online
OpenStudy (anonymous):

fibonacci sequence....i found this code and it really worked but i don't understand how code was excuted. Thank

OpenStudy (anonymous):

OpenStudy (anonymous):

OpenStudy (anonymous):

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

OpenStudy (anonymous):

This is the one i couldn't understand. Can you explain to me the idea please. Thanks

OpenStudy (anonymous):

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

OpenStudy (anonymous):

I can't understand old_a and old_b issues

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

sammi789 is a spammer and i would bet her website doesn't specifically cater to the MIT OCW class like this one does.

OpenStudy (anonymous):

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...

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!