kishanrajput23/NPTEL-The-Joy-of-Computing-using-Python

week 4 Q9

Closed this issue · 1 comments

please explain solution

a=0
b=1
print(a, end="")
for i in range(0, 15, 1):
    print(b, end="")
    a, b = b, a+b

The given program will print 01123581321345589144233377610 and This is fibonacci series.

A Fibonacci sequence is the integer sequence of 0, 1, 1, 2, 3, 5, 8....

The first two terms are 0 and 1 i,e, we already declared it in variable a and b. All other terms are obtained by adding the preceding two terms. This means to say the nth term is the sum of (n-1)th and (n-2)th term.

so, the 3rd lind will print first the value of a i,e 0 then in the first iteration of for loop b will be print i,e 1 and at this point our series is 0,1.

After printing b, the value will change in 6th line a,b = b, a+b and earlier we have 0 as value of a but now it changes to 1 as value of a and earlier we have 1 as value of b but it now changes to a+b i,e 0+1 = 1.

so, finally we have now a =1 and b=1

now in 2nd iteration of for loop b again will print and now b has value of 1. so at this point our series will look like : 0, 1, 1

Again after printing b the values will gain change now a has value of 1 beacaue a = b and b has value of 2 because b = a+b i,e b = 1+1.

after that our series will look like : 0, 1, 1, 2

and hence like this series will continue 15 times and finally at last it gives us series like : 01123581321345589144233377610

Hope you understand. If not then ask your query again.