Need explanation for Fibonacci sequence code

This is the place for queries that don't fit in any of the other categories.

Need explanation for Fibonacci sequence code

Postby Siege » Tue May 13, 2014 10:39 am

Hi everyone, I am fairly new to programming, studying using the Non-Programmer's Tutorial for Python 3 Wikibook and need help on these two Fibonacci code :
Code: Select all
a = 0
b = 1
count = 0
max_count = 20
while count < max_count:
    count = count + 1
    print(a, end=" ")

    old_a = a
    a = b
    b = old_a + b
print()
and
Code: Select all
a = 0
b = 1
count = 0
max_count = 10
 
while count < max_count:
    count = count + 1
    print(a, b, end=" ") 
    a = a + b   
    b = a + b
print()
I do not understand how they derive the Fibonacci sequence and especially how it got 0 1 1 at the start of the Fibonacci sequence. Just so everyone knows that I haven't been taught the Fibonacci sequence but knows what it is about.
Last edited by Yoriz on Tue May 13, 2014 11:57 am, edited 1 time in total.
Reason: First post lock.
Siege
 
Posts: 8
Joined: Tue May 13, 2014 10:22 am
Location: Pasir Ris,Singapore

Re: Need explanation for Fibonacci sequence code

Postby Crimson King » Tue May 13, 2014 4:47 pm

Hey Siege,

The Fibonacci sequence works as follows:


Code: Select all
 n =    0    1    2    3    4    5    6    7    8    9    10    11    12    13    14    ...
xn =    0    1    1    2    3    5    8    13    21    34    55    89    144    233    377    ...



You get the Xn number by adding Xn-1 + Xn-2. For example if you wanted to get the 7th number in the sequence (13) you have to know the 5th and 6th (which are 5 and 8 respectively)

The code you posted does this:

Code: Select all
First time it enters the while loop:
prints 0 1 #these are the values set at the start of your script
a = 0 + 1
b = 1 + 1
Second time it enters the while loop:
prints 1 2 # these were assigned in the first loop but printed on the second
a = 1 + 2
b = 3 + 2

And so on


Let us know if that helped.

Cheers!
User avatar
Crimson King
 
Posts: 85
Joined: Fri Mar 08, 2013 2:42 pm
Location: Buenos Aires, Argentina

Re: Need explanation for Fibonacci sequence code

Postby 7stud » Wed May 14, 2014 2:41 am

If you are having trouble following the values of variables, get a piece of paper and label a column with each variable name, then step through your code:


Code: Select all
a = 0
b = 1
count = 0
max_count = 20
while count < max_count:
    count = count + 1
    print(a, end=" ")

    old_a = a
    a = b
    b = old_a + b
print()


---just before the while loop starts:
Code: Select all
 a        b
___      ___   
 0        1


--while loop starts---
Code: Select all
print(a, end=" ")  ==> 0

  old_a
---------
 old_a = a = 0

 a                b
___              ___   
 0                1
 a = b = 1        b = old_a+b = 0+1 = 1


---execution goes to top of while--

Code: Select all
print(a, end=" ")  ==> 1

  old_a
---------
   0
 old_a = a = 1

 a                 b
___               ___   
 0                 1
 1                 1
 a = b = 1         b = old_a + b = 1+1 =2

---execution goes to top of while--

Code: Select all
print(a, end=" ")  ==> 1
7stud
 
Posts: 85
Joined: Wed Apr 02, 2014 2:36 am

Re: Need explanation for Fibonacci sequence code

Postby Siege » Wed May 14, 2014 9:05 am

Thanks to both Crimson King and 7stud for helping me to understand the code but I still have a problem, though. Why do we have to type this function :
Code: Select all
print()
at the end of the code when it does not even create a new empty line or do I not need to type that in ? I tried running both programs without
Code: Select all
print()
and it still returns me the same output :
Code: Select all
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
Siege
 
Posts: 8
Joined: Tue May 13, 2014 10:22 am
Location: Pasir Ris,Singapore

Re: Need explanation for Fibonacci sequence code

Postby Kebap » Wed May 14, 2014 9:27 am

Hi Siege, welcome to the forums and programming in general! In your code, you print all the numbers in the same line. So after you are finished, you use print() to end that line. If you remove it, you will still see the same numbers (they are printed during the loop not after). Try changing the last line print() to print("Test") and see if you notice any differences. :geek:
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 375
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: Need explanation for Fibonacci sequence code

Postby Siege » Wed May 14, 2014 9:55 am

TY, Crimson King, 7stud and Kebap for helping me fully understand the code ! :D
Siege
 
Posts: 8
Joined: Tue May 13, 2014 10:22 am
Location: Pasir Ris,Singapore

Re: Need explanation for Fibonacci sequence code

Postby Kebap » Wed May 14, 2014 10:09 am

You are very welcome. Feel free to start a new thread if you have any other doubts or questions. Happy programming! :D
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 375
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests