3

1-

 def fib1(n):
     a = 0
     b = 1
     while a < n:
        print b
        a = b
        b = a+b

2-

 def fib2(n):
     a, b = 0,1
     while a < n:
         print b
         a,b = b, b+a

On execution:

fib1(10) I got the wrong answer: 0 1 2 4 8

fib2(10) I got the right answer: 0 1 1 2 3 5 8

2
  • sorry i ment def :) typing error Commented Apr 20, 2016 at 13:32
  • Seen this exact same question posted here awhile ago. Where did the question come from ? Commented Apr 20, 2016 at 13:56

3 Answers 3

8

In fib 1 a = b overwrites the value of a,

which means a is no longer the right value for the statement

b = a+b

However, in your second example both those things happen at the same time on the line a,b = a, b+a which means a is the right value still.

Sign up to request clarification or add additional context in comments.

5 Comments

what i can't git it is, how those things happen at the same time?, i guess in both fib1() and fib2() a = b will overwrite the value of a with the new b = a+b.
The RHS happens before the assignment to the left hand side. so B+A will be computed, then B will be computed, then b will be assigned to a + b, and a will be assigned to b
For example, when you say b = a + b the first thing that happens is that a+b is computed and stored somewhere tempory in memory. After that computation the value is moved in to be. The same thing happens in this case a,b = b, b+a first, b+a is computed and stored somewhere temporary. Then b is computed and stored somewhere temporary. Finally b = the computed value of a+b) and a = the computed value of b
finally got it, it's very helpful indeed, thanks :) one last question, why the first returned value in fib2() is 0 ? i guess it should be 1.
In my experience running the code, it does not output a zero first. my output of fib2(10) is 1 1 2 3 5 8 13
0

here's a quick answer:

the basic difference is the way the values of a and b are reassigned. In fib1(), you have

a = b
b = a + b

while in fib2(), you have

a, b = b, b + a

Now, these two looks equal statements but they are not. Here's why:

In fib2(), you assign the values of the tuple (b, b + a) to the tuple (a, b). Hence, reassignment of values are simultaneous.

However, with fib1(), you first assign value of b to a using a = b and then assign the value a + b to b. Since you have already changed value of a, you are in effect doing

b = a + b = b + b = 2b

In other words, you are doing a, b = b, 2b and that is why you are getting multiples of 2 rather than the fibonacci sequence.

Comments

0

fib1 contains a classic bug. It is in the same realm as that of swapping values of two variables. Think about how you would have done that in C or C++.

int a = 3;
int b = 5;
int temp;
temp = a; /* 3 */
a = b; /* 5 */
b = temp; /* 3, hence swapped */

There is a way to do without temp, although there are intermediate calculations involved. Now in Python, if you are not going to exploit the tuple unpacking feature, you have to involve a temp variable.

a = 3
b = 5
temp = a
a = b
b = temp

OR

a = 3
b = 5
a_ = (a+b)/2 - (a-b)/2 # 5.0
b_ = (a+b)/2 + (a-b)/2 # 3.0

Better use the tuple unpacking as in fib2.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.