473,396 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

beginner question fibonacci


# Fibonacci series: .... # the sum of two elements defines the next
.... a, b = 0, 1 while b < 10: .... print b
.... a, b = b, a+b
....
1
1
2
3
5
8
a, b = 0, 1
while b < 10:

print b
a = b
b = a+b
1
2
4
8

Why a, b = b, a+b isn't a = b; b = a+b ?
Jul 21 '05 #1
4 1076
Joon wrote:
>>> a, b = 0, 1
>>> while b < 10:

print b
a = b
b = a+b
1
2
4
8

Why a, b = b, a+b isn't a = b; b = a+b ?


Because you changed a before you added it to b.

Let's call your existing a and b "a0" and "b0", and the next a and b
"a1" and "b1". When you do a, b = b, a+b you are assigning:

a1 = b0
b1 = a0 + b0

But when you use separate statements, you are assigning:

a1 = b0
b1 = a1 + b0 = b0 + b0 = 2*b0
--
Michael Hoffman
Jul 21 '05 #2
Joon wrote:
>>> # Fibonacci series: ... # the sum of two elements defines the next
... a, b = 0, 1 >>> while b < 10: ... print b
... a, b = b, a+b
...
1
1
2
3
5
8
>>> a, b = 0, 1
>>> while b < 10:

print b
a = b
b = a+b

1
2
4
8

Why a, b = b, a+b isn't a = b; b = a+b ?


It's actually equivalent to:

temp = (b, a+b)
a = temp[0]
b = temp[1]

The temporary tuple object is created first, with the old values of a
and b. Then a and b are reassigned. The value of a doesn't change until
*after* a+b is calculated.

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Jul 21 '05 #3
The case is that Python in attribution commands solves first the right
side, so he atributes the vars.

So the a+b expression is executed first.

Joon escreveu:
# Fibonacci series: ... # the sum of two elements defines the next
... a, b = 0, 1 >>> while b < 10: ... print b
... a, b = b, a+b
...
1
1
2
3
5
8
>>> a, b = 0, 1
>>> while b < 10:

print b
a = b
b = a+b
1
2
4
8

Why a, b = b, a+b isn't a = b; b = a+b ?


Jul 21 '05 #4
Yes, i see.
Thank you very much for the fast help!
Jul 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: dleecurt | last post by:
Hello, I have a small problem, I am trying to write a program that will calculate the Fibonacci number series, and I have the code complete with one problem. I used a long in to store the numbers,...
0
by: Alex Vinokur | last post by:
An algorithm which computes very long Fibonacci numbers http://groups.google.com/groups?selm=bnni5p%2412i47o%241%40ID-79865.news.uni-berlin.de was used as a performance testsuite to compare speed...
5
by: Lance | last post by:
What is the correct STL libraries to implement in order to have a Fibonacci Heap created? I am creating an algorithm which would use a Fibonacci Heap. Thanks, Lance
5
by: Niks | last post by:
Can anybody explain me what is a "Fibonacci search"? even an URL will do. Thanks for reading.
4
by: YS Sze | last post by:
If you know the exact longitude and latitude for a specific location, would anyone think it'd make any sense to find out if this set of location numbers is really part of the Fibonacci series or...
12
by: CII | last post by:
Hi everybody. I've been reading posts a year old about the fibonacci series right on this newsgroup, and while it's not directly games related, I'll share my own notes as well. On another...
14
by: felixnielsen | last post by:
Im actually kinda embarassed to ask this question... @code start #include <iostream> int main() { unsigned long long a = 1; unsigned long long b = 1; for (int i = 0; i < 45; i++) { a += b;...
3
by: greek | last post by:
Hi! I hav to generate fibonaaci series using recursion: 0,1,1,2,3,5,8,18,21... whr fibonacci(0)=0 fibonacci(1)=1 fibonacci(n)=fibonacci(n-1)+fibonacci(n-2) ive witten the code but having 2...
13
by: mac | last post by:
Hi, I'm trying to write a fibonacci recursive function that will return the fibonacci string separated by comma. The problem sounds like this: ------------- Write a recursive function that...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.