-1

Dear members I have a query, I understand the concept from the below :

https://stackoverflow.com/a/21990958/22419694 Python a, b = b, a +b

but what if we code the program this way ?

n=int(input("Enter a number"))
def fibo(n):
    b=1
    a=0
    for i in range (n+1):
        print(a)
        b=a+b # b =0+1 ( a is not yet initialized to 1)which makes   b=1
        a=b   # and then b is initialized with 'a' as per my logic so, a=1
print(fibo(n))

Please help

I am trying to execute Fibonacci series but still I fail to get the desired result i.e Fibonacci series for n=5 0,1,1,2,3,5... instead the result is = 0,1,2,4,8

Bill Hileman
  • 2,798
  • 2
  • 17
  • 24
Jagadish
  • 31
  • 3
  • @isedev can you please help me through , thanks – Jagadish Aug 22 '23 at 05:32
  • 2
    `a, b = b, a+ b` performs simultaneously. The right side is evaluated, and then these two values are assigned to `a` and `b`. In the second case, you are assigning `a + b` to `b`, and then setting `a` to this new value of `b`, not the old value of `b`, – Frank Yellin Aug 22 '23 at 05:35
  • @Jagadish: this was actually already explained in the post you linked! But indeed, think **simultaneously** instead of **sequentially**. – Swifty Aug 22 '23 at 05:54
  • 2
    add `print(i,a,b)` after every line if you have trouble following through... or use a step through debugger – Julien Aug 22 '23 at 05:54

2 Answers2

0

In your code
b=a+b -> Here new 'b' value is set to the previous 'a' value + previous 'b' value.
a=b -> And you assign the new 'b' value, which is assigned in the above step, as the new 'a' value instead of setting the previous 'b' value. This is wrong.

Try below code

n=int(input("Enter a number"))
def fibo(n):
    a=0
    b=1
    for i in range (n+1):
        print(a)
        c = a # to store previous a value
        a = b # assign the previous b to the new a 
        b = c + b # assign previous a + previous b to new b                  
fibo(n)

output

Enter a number5
0
1
1
2
3
5
KRG
  • 655
  • 7
  • 18
0

Main point: when a = 10 and b = 20 then,
a, b = b, a results in a = 20 and b = 10 whereas,
a=b;b=a results in a = 20 and b = 20

Fibonacci: 0, 1, 0+1, (0+1)+1, (0+1) + ((0+1)+1)
variables: a, b, a+b, (a+b)+b, (a+b) + ((a+b)+b)
simplified: a, b, (a+b)(or c), (c+b)(or d), d+c
value of a is passed to b and b is set to a+b

  • step 1: a=0,b=1, output:0
  • step 2: a=,b=1, output:1
  • step 3: a=1,b=2, output:1
  • step 4: a=2,b=3, output:2
  • step 5: a=3,b=5, output:3

but what if we code the program this way ? Your code:

n=int(input("Enter a number"))
def fibo(n):
    b=1
    a=0
    for i in range (n+1):
        # a = 1, b = 1
        print(a) # 1 
        b=a+b # 2+2
        a=b # 4
        # a = 2, b = 2
print(fibo(n))

I am trying to execute Fibonacci series but still I fail to get the desired result i.e Fibonacci series for n=5 0,1,1,2,3,5... instead the result is = 0,1,2,4,8

What does your code do:
sets b to a+b
and same a+b value to a
it never swaps the value

  • step 0: a=0,b=1 output:0
  • step 1: a=1,b=1, output:1
  • step 2: a=2,b=2, output:2
  • step 3: a=4,b=4, output:4
  • step 4: a=8,b=8, output:8
  • step 5: a=16,b=16, output:16

For swap you must have temp variable like for classic way

# a = 1, b=2
temp = a + b 
a = b 
b = temp
# a = 2, b=3

or pythonic way to implement:

# a = 1, b=2
a, b = b, a+b
# a = 2, b=3
Bibhav
  • 1,579
  • 1
  • 5
  • 18