#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    1
    Rep Power
    0

    Fibonacci sequence example has be slightly confused


    >>> # 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


    --------------------

    This doesn't entirely make sense to me and I was wonderfing if someone could help explain it. I've just got started in programming and I don't understand how a & b are being assigned completely different variables and how it would make it calculate itself in a continuous loop if it weren't for the while b < 10:
    thanks,
    NightStalkerX6
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Hey Night,

    First you need to understand that anything after a # up untill the end of the line is a comment, so just forget about them .

    The program first assigns the value 0 to a and 1 to b, next it starts a while loop which will continue untill b is not less than 10. Every time the loop runs though it prints the value of b then assignes that value to a and gives b the value of a + b

    In theory the loop would keep going, adding a and b and printing the output, anyway i hope that's cleared things up for you

    Welcome to Python, have fun!

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Hi, I'm Calvin
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    LosAngeles, SanDiego, Houston
    Posts
    50
    Rep Power
    11
    wassup NightStalker,

    it helps to understand how a and b are assigned different values if you re-write the code like this:

    Code:
    a=0
    b=1  #separate assignments
    
    while (b < 10):
      print b
      a = b
      b = a + b
    i'm not sure if that's exactly what you were asking... if you look through the tutorials for python though, you could easily understand the concept of the while loop
  6. #4
  7. No Profile Picture
    Hi, I'm Calvin
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Location
    LosAngeles, SanDiego, Houston
    Posts
    50
    Rep Power
    11

    oops, i goofed


    in a statement such as this:
    a,b = b,a+b

    ...python evaluates all expressions (ie b and a+b) on the right hand side before assigning any values to a or b. the previous example that i gave trying to clarify the assignments was incorrect, so sorry!

    it's more like this:
    Code:
    a=0
    b=1 #separate assignments
    while(b<10):
      A = b
      B = a+b #eval before assigning
      a=A
      b=B #assign after evaluating
    sorry yall.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2005
    Posts
    1
    Rep Power
    0
    This is the simplest way to do it

    Variables a, b, c, d

    a = 1
    b = 0
    c = 1

    While c < n # number of repetitions

    c = c + 1

    Print a

    d = b
    b = a
    a = b + d

    Loop
  10. #6
  11. Caress me down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Location
    Pennsylvania
    Posts
    289
    Rep Power
    511
    this is off topic but what is the number called if you divide 2 numbers of the fib sequence together? like 89/55 gets you close to some irrational number. as you move down the fib sequence you get closer and closer to the irrational number.

    it has something to do with pentagons too... and other shapes like a dodecahedran.
    so you think you can tell heaven from hell
  12. #7
  13. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    It's called the Golden Ratio.

    Comments on this post

    • lw22 agrees : thanks
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo