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

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 = 0b = 1count = 0max_count = 20while count < max_count:    count = count + 1    print(a, end=" ")    old_a = a    a = b    b = old_a + bprint()`
and
Code: Select all
`a = 0b = 1count = 0max_count = 10 while count < max_count:    count = count + 1    print(a, b, end=" ")      a = a + b        b = a + bprint() `
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

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 scripta = 0 + 1b = 1 + 1Second time it enters the while loop: prints 1 2 # these were assigned in the first loop but printed on the seconda = 1 + 2b = 3 + 2And so on`

Let us know if that helped.

Cheers!
RIP #!

Crimson King

Posts: 159
Joined: Fri Mar 08, 2013 2:42 pm
Location: Dublin, Ireland

### Re: Need explanation for Fibonacci sequence code

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 = 0b = 1count = 0max_count = 20while count < max_count:    count = count + 1    print(a, end=" ")    old_a = a    a = b    b = old_a + bprint()`

---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: 106
Joined: Wed Apr 02, 2014 2:36 am

### Re: Need explanation for Fibonacci sequence code

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

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.

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: 571
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

### Re: Need explanation for Fibonacci sequence code

TY, Crimson King, 7stud and Kebap for helping me fully understand the code !
Siege

Posts: 8
Joined: Tue May 13, 2014 10:22 am
Location: Pasir Ris,Singapore

### Re: Need explanation for Fibonacci sequence code

You are very welcome. Feel free to start a new thread if you have any other doubts or questions. Happy programming!

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: 571
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe