Thread: nth fibonacci

  1. #1
    Registered User meriororen's Avatar
    Join Date
    Dec 2008
    Posts
    22

    Question nth fibonacci

    can someone explain to me how this program calculates nth fibonacci number? I scribbled for 2 hours now and stuck
    hints greatly appreciated.
    Code:
    int f (unsigned int n) {
        int x = 0, y = 1, a = 1, b = 0, t;
        for (;;) {
            if (n & 1) {
                t = a;
                a = a * x + b * y;
                b = t * y + b * (x + y);
            }
            n >>= 1;
            if (n == 0) return b;
            t = x;
            x = x * x + y * y;
            y = t * y + y * (t + y);
        }
    }
    Thanks.
    Baka nanka janai. Shoshinsha tte iu mon da.

  2. #2
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    Looks like nobody has a clue.

    To the hints,
    if (n & 1)
    checks to see if n is odd number. And
    n >>= 1;
    is like n /= 2;

    So it goes.
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fibonacci prime
    By dbzx in forum C Programming
    Replies: 5
    Last Post: 04-17-2009, 11:13 AM
  2. fibonacci using recursion?
    By n3cr0_l0rd in forum C Programming
    Replies: 12
    Last Post: 02-25-2009, 08:49 AM
  3. Recursive Fibonacci, some help needed
    By cwafavre in forum C Programming
    Replies: 8
    Last Post: 11-04-2007, 02:20 PM
  4. fibonacci problem using while loop
    By galmca in forum C Programming
    Replies: 6
    Last Post: 10-02-2004, 05:12 AM
  5. Fibonacci series using a recursive function
    By Dargoth in forum C++ Programming
    Replies: 3
    Last Post: 02-05-2002, 12:54 AM