Home > database >  Fibonacci Sequence using For Loop - Don't understand the loop
Fibonacci Sequence using For Loop - Don't understand the loop

Time:01-04

Very new to python and I am trying to understand how I am getting the correct answer

Write the code that:

  1. Calculates and prints the first 50 terms of the fibonacci sequence.
  2. Print each term and number as follows:
term: 0 / number: 0
term: 1 / number: 1
term: 2 / number: 1
term: 3 / number: 2
term: 4 / number: 3
term: 5 / number: 5

I got the right answer but how does the "else" part work?

c = a   b
print(f'term: {term} / number: {c}')
a = b
b = c

Can someone explain to me exactly how the variable above changes each time it loops? I don't understand how c = 1 1 which prints c = 2 but how does it print c = 3 afterwards?

a = 1
b = 1
for term in range (50):
    if term <= 1:
        print(f'term: {term} / number: {term}')
    elif term == 2:
        print(f'term: {term} / number: 1')
    else:
        c = a   b
        print(f'term: {term} / number: {c}')
        a = b
        b = c

CodePudding user response:

As you noted, the first time we proceed through the else part, we have the following assignment, we apply the following assignments:

c = a   b # <-- (1)   (1) = 2
a = b     # <-- (1)
b = c     # <-- (2)

So after the first traversal through the else portion, b is the most recent fibonacci number (in this case 2) and a is the one before. The next time, we do the following.

c = a   b # <-- (1)   (2) = 3
a = b     # <-- (2)
b = c     # <-- (3)

With this, b has been updated to become the next Fibonacci number, and a is updated to become 2, which was the most recent Fibonacci number but is now the one before.

In this fashion, c is always the most recent Fibonacci number, and this value is assigned to b for use in the next loop, and that value of b is eventually assigned to a.


Incidentally, you can avoid the logic of the if/elif/else block if you refactor your code into any of the following:

a = 1
b = 1
print('term 0 / number: 0')
print('term 1 / number: 1')
print('term 2 / number: 1')

for term in range (3,50):
    c = a   b
    print(f'term: {term} / number: {c}')
    a = b
    b = c

Alternatively,

a = 0
b = 1
for term in range (50):
    print(f'term: {term} / number: {a}')
    c = a   b
    a = b
    b = c

The following also works and removes the variable c:

a = 0
b = 1
for term in range (50):
    print(f'term: {term} / number: {a}')
    (a, b) = (b, a b)

CodePudding user response:

Fibonacci Series

In mathematics, the Fibonacci numbers, commonly denoted Fn , form a sequence, the Fibonacci sequence, in which each number is the sum of the two preceding ones. The sequence commonly starts from 0 and 1, although some authors start the sequence from 1 and 1 or sometimes (as did Fibonacci) from 1 and 2. Starting from 0 and 1, the first few values in the sequence are:[1]

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144.

Therefore we can see the to get the next number we are adding the previous two numbers.

Now lets dry run your code

a = 1
b = 1
for term in range (50):
    if term <= 1:
        print(f'term: {term} / number: {term}')
    elif term == 2:
        print(f'term: {term} / number: 1')
    else:
        c = a   b
        print(f'term: {term} / number: {c}')
        a = b
        b = c

The first two lines:-

a = 1
b = 1

are specifying the second and third two values in the series.so that we can use them in addition to get the next term.

Then in the 3rd line:-

for term in range (50):

we are initiating a for loop that would go on 50 times as it is specified in range(50): Note:- This controls the number of terms printed in series. change the number in range to the number of terms you want to print.


The next if statment:-

if term <= 1: print(f'term: {term} / number: {term}') this if statement deals with the condition for the first two terms. where the value of term is 0 and 1. we used the variable term which is in for loop in the print statement to print the term and its value.


the elif statement:-

elif term == 2: print(f'term: {term} / number: 1')

we are using this elif statement to print the third term. as we need the third term to be 1 (0 1) we are hard-typing it into the print statement using elif statement.


Now the else statement you have asked for:

 else:
    c = a   b
    print(f'term: {term} / number: {c}')
    a = b
    b = c

This else statement deals with 4th term and terms after it.

c=a b

will add the previous two terms as required in Fibonacci series.

print(f'term: {term} / number: {c}')

this will print the term and value.

then,

a = b
b = c

will update the values to the next term. and the loop restarts with new values.

  • Related