fibonacci sequence - getting false results with unsigned long

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • coleslaw01
    New Member
    • Mar 2008
    • 3

    fibonacci sequence - getting false results with unsigned long

    Hello, I am trying to teach myself C++ while babysitting a stable network in Iraq and have put together a program to display the fibonacci sequence. It works with long and long double(output in exponent though)

    After sequence 47 using long type the program won't display the output properly (I am assuming because of size limit of type) so I switched type to unsigned long and now number 49 outputs as 512559680 when it should be 4807526976

    Is there something wrong with my code, or is there something about using unsigned long that I am unaware of? Also is there anyway to output long double in normal notation to view the sequence further down? Any help would be greatly appreciated.

    Code:
    #include <iostream>
    
    using std::cin;
    using std::cout;
    using std::endl;
    
    unsigned long fib(unsigned long, unsigned long);
    
    int main()
    {
        unsigned long f0 = 0;
        unsigned long f1 = 1;
        int counter = 0;
          
        while (counter <= 46)
        {
              if (f0 == 0)
                   {
                   cout << "Sequence 1: " << f0 << endl;
                   cout << "Sequence 2: " << f1 << endl;
                   f0 = f1 + f0;
                   counter = 2;
                   cout << "Sequence 3: " << f0 << endl;
                   } 
              f0 = fib(f0, f1);
              cout << "Sequence " << counter + 2<< ": " << f0 << endl;
              counter++;
              f1 = fib(f1, f0);
              cout << "Sequence " << counter + 2<< ": " << f1 << endl;
              counter++;
         }
    
         cout << endl << endl;
         system("PAUSE");
         return 0;
    }
    
    unsigned long fib(unsigned long x, unsigned long y)
    {
         return(x+y);
    }
    Coleslaw01
  • DaemonCoder
    New Member
    • Mar 2008
    • 43

    #2
    Code:
     unsigned long f0 = 0;
        unsigned long f1 = 1;
        int counter = 0;
          
        while (counter <= 46)
        {
              if (f0 == 0)
                   {
                   cout << "
    You said after 47 the numbers get jumbled... The counter goes to 46....
    The reason your number is high is the double increment in the loop...

    Code:
        counter++;
              f1 = fib(f1, f0);
              cout << "Sequence " << counter + 2<< ": " << f1 << endl;
              counter++;
         }
    That is also why your loop gets to 47.. when the limit is 46, because the even numbers are skipped on iterations.

    Hope this helps....

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      Your while condition restricts to 46

      Comment

      • MACKTEK
        New Member
        • Mar 2008
        • 40

        #4
        According to Microsoft (I am using C++ in VS 2005)
        Here are the ranges for numbers
        An unsigned long's max is 4,294,967,295.

        Once you hit sequence 48: 2,971,215,073
        You came over half way to the Max.
        Thus on your next addition, you would have exceeded your ability to "hold" the number in an unsigned long.

        In c++.Net there is something called Decimal. I ran the code in a C++.Net and it seemded to work fine.

        I supposed you can also look for a different ANSI type as well, like "unsigned long long"

        Also
        I just increased your counter to get the higher numbers, and Using Decimal as the type of number:
        #50 was 7,778,742,049
        #51 was 12,586,269,025

        Comment

        • coleslaw01
          New Member
          • Mar 2008
          • 3

          #5
          Sweet. Thanks a bunch for clearing that up for me. I guess I'll have to look/read around for a different variable type as I don't have .net Does anyone know if Dev C++ supports decimal or has a different name for it?

          Comment

          • MACKTEK
            New Member
            • Mar 2008
            • 40

            #6
            Check out "long long" and maybe "unsigned long long"

            My guess is those are supported.

            Comment

            Working...