Thread: fibonacci sequence

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by master5001 View Post
    I am looking at some equations for calculating arbitrary fibonacci numbers and I am not seeing anything terribly like the one which you are using. Your equation itself is wrong. That is why your output is wrong. And as its been said several times over, iteratively calculating these numbers is not a terrible chore and it need only be done once, tabled, and used on-demand.

    You're a nut. None of the output from any of these solutions was ever wrong and nobody ever claimed that either...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I was talking to the original poster, and I never said I was a nut, but I am saying I am not wrong. The OP asked how come his output was wrong, and I am stating why.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by master5001 View Post
    I was talking to the original poster, and I never said I was a nut, but I am saying I am not wrong. The OP asked how come his output was wrong, and I am stating why.
    All apologies. And by "nut" I meant interesting...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well I am a nut. And its all good. I took out my calculator and tried some stuff with his equation by hand, and it doesn't even calculate the third number correctly

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I took out my calculator and tried some stuff with his equation by hand, and it doesn't even calculate the third number correctly
    Good for you. I couldn't even get it to compile since M_E is undefined for me

    That said, going by the webpage referenced, the formula is correct (though that webpage's formula is itself an approximation, not the full formula).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    #include <math.h>, laserlight. I will double check, but unless I did something else wrong, I didn't get the formula to work correctly. The point is moot though, since its not the most efficient way of solving this. Plus it would only be beneficial to use a formula like this to solve an arbitrarily high number, which cannot be done due to accuracy issues, apparently.

    [edit]Wtf... *#include <float.h> I mean[/edit]

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I am still not getting this to work. I get what the derivation is saying and all, and it looks fascinating. Perhaps I am just not doing it correctly. asinh() is a bastard to do on my calc.. But still, I did it correctly -_-

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I will double check, but unless I did something else wrong, I didn't get the formula to work correctly.
    Perhaps you would like to try this, using just square root computation instead of a trigonometric function:
    Code:
    #include <math.h>
    #include <stdio.h>
    
    unsigned int fib(unsigned int n)
    {
        double sqrt5 = sqrt(5.0);
        double phi = (1.0 + sqrt5) / 2.0;
        return (unsigned int)((pow(phi, n) / sqrt5) + 0.5);
    }
    
    int main(void)
    {
        int x;
    
        for (x = 0; x < 48; ++x)
        {
            printf("&#37;u: %u\n", x, fib(x));
        }
    
        return 0;
    }
    Ideally, we should only compute sqrt5 and phi once, but anyway...

    The point is moot though, since its not the most efficient way of solving this.
    That depends on what factors you are considering for efficiency.

    Plus it would only be beneficial to use a formula like this to solve an arbitrarily high number, which cannot be done due to accuracy issues, apparently.
    Yes, and it is not as efficient as memoization when you want to generate all the Fibonacci numbers up to some limit, like what cph was trying to do.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You know what laserlight, I had an unfortunate IO error (That would be "Idiot Operator" not "Input/Output"). Yeah it works fine.. I am just a boob sometimes.

    And yeah I tried the non-trigonometric version when I was looking at the derivation which is why I wasn't following how come my results weren't working. But it works fine.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by cph
    I need to get the right result from the function fib(), because when n=71, n=76, and after n=79 the function returns incorrect result (f[n]!=f[n-1]+f[n-2]).
    64-bit numbers?
    GMP?

    You are asking how to reach above and beyond a hardware limitation. You are asking how to make your computer do something that is beyond the implementation of your algorithm. Right? I mean you see how I arrive at that conclusion, right?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nonoob
    += associativity is right-to-left just as any assignment, so I assume it is executed in that order.
    Order of evaluation and associativity are not the same thing. However, I think that matsp is wrong - there is no undefined behaviour as far as I can tell. I recall a thread some time ago where the conclusion was that the XOR swap of a and b written as a ^= b ^= a ^= b; resulted in undefined behaviour, but I believe that is because a is modified twice within consecutive sequence points. In your code, however, both a[0] and a[1] are modified exactly once within consecutive sequence points.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Thanks for that explanation, laserlight. i was starting to get worried that I am misunderstanding associativity in any standard precedence chart. What is it then if "right-to-left" is not related to execution order? The right argument is evaluated first, I thought.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nonoob
    What is it then if "right-to-left" is not related to execution order? The right argument is evaluated first, I thought.
    The order of evaluation of operands is unspecified. In this case, it does not matter, but consider this contrived example:
    Code:
    #include <stdio.h>
    
    int *foo(int *x, int y)
    {
        printf("&#37;d\n", y);
        return x;
    }
    
    int main(void)
    {
        int a = 1;
        int b = 2;
        *foo(&a, 1) += *foo(&b, 2) += *foo(&a, 3);
        return 0;
    }
    It is possible that on one implementation the output is "1\n2\n3\n" while on another it is "3\n2\n1\n".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Apr 2009
    Posts
    8
    Sorry to bump an old thread, but I came accross this accidentally and wanted to share my thoughts.

    First of all, the problem with this method posted above:
    Code:
    pow(PHI, n) / sqrt(5.0)
    is that it's simply incorrect, or incomplete. It's only half of the Fibonacci recurrence relation.

    The correct way would be:
    Code:
    int Fib( int n )
    {
      static const double s = sqrt(5.0);
      return int( (pow(1+s,n)-pow(1-s,n))/(pow(2.0,n)*s) );
    }
    (use long long instead of int for large n, as the result will obviously not fit in 32 bit integers).

    There is, however, an even much faster way. After quite some experimenting with several approaches and lotsa optimizing, this is imho the ultimate solution: (besides using a direct lookup or caching table of course)
    Code:
    unsigned int Fib( unsigned int n )
    {
      n--;
      unsigned int a=1,b=0,c=1,p=1,t;
      while (p<=n) p += p;
      do
      {
        p >>= 1;
        t = b*b;
        b *= a+c;
        a = a*a + t;
        c = c*c + t;    
        if (n&p)
        {
          c = b;
          b = a;
          a += c;
        }
      }
      while (p>1);
      return a;
    }
    This performs about 3 times as fast as the pow / √5 method. Of course, again, use long long for large n.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Phuzzillogic
    Sorry to bump an old thread, but I came accross this accidentally and wanted to share my thoughts.
    That is okay; I maintain that it is fine to post in an ancient thread when you have something worthwhile to contribute, especially if it is an important correction.

    Quote Originally Posted by Phuzzillogic
    is that it's simply incorrect, or incomplete. It's only half of the Fibonacci recurrence relation.
    It is not incorrect in its idea since the idea is that the other half can be ignored as it has negligible contribution in the approximation. I demonstrated a correct way to perform this approximation in post #45.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fibonacci Sequence
    By Dogmasur in forum C Programming
    Replies: 15
    Last Post: 08-10-2008, 07:55 AM
  2. Fibonacci sequence
    By MuffanMan123 in forum C++ Programming
    Replies: 6
    Last Post: 02-26-2008, 09:15 AM
  3. Immediate programming help! Please!
    By xMEGANx in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2008, 12:52 PM
  4. Fibonacci sequence output statement
    By chocha7 in forum C++ Programming
    Replies: 10
    Last Post: 11-18-2004, 11:04 PM
  5. Fibonacci sequence
    By girliegti in forum C++ Programming
    Replies: 8
    Last Post: 09-30-2003, 10:40 PM