473,395 Members | 1,905 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,395 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using std::cin;
  4. using std::cout;
  5. using std::endl;
  6.  
  7. unsigned long fib(unsigned long, unsigned long);
  8.  
  9. int main()
  10. {
  11.     unsigned long f0 = 0;
  12.     unsigned long f1 = 1;
  13.     int counter = 0;
  14.  
  15.     while (counter <= 46)
  16.     {
  17.           if (f0 == 0)
  18.                {
  19.                cout << "Sequence 1: " << f0 << endl;
  20.                cout << "Sequence 2: " << f1 << endl;
  21.                f0 = f1 + f0;
  22.                counter = 2;
  23.                cout << "Sequence 3: " << f0 << endl;
  24.                } 
  25.           f0 = fib(f0, f1);
  26.           cout << "Sequence " << counter + 2<< ": " << f0 << endl;
  27.           counter++;
  28.           f1 = fib(f1, f0);
  29.           cout << "Sequence " << counter + 2<< ": " << f1 << endl;
  30.           counter++;
  31.      }
  32.  
  33.      cout << endl << endl;
  34.      system("PAUSE");
  35.      return 0;
  36. }
  37.  
  38. unsigned long fib(unsigned long x, unsigned long y)
  39. {
  40.      return(x+y);
  41. }
  42.  
Coleslaw01
Mar 7 '08 #1
5 2441
Expand|Select|Wrap|Line Numbers
  1.  unsigned long f0 = 0;
  2.     unsigned long f1 = 1;
  3.     int counter = 0;
  4.  
  5.     while (counter <= 46)
  6.     {
  7.           if (f0 == 0)
  8.                {
  9.                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...

Expand|Select|Wrap|Line Numbers
  1.     counter++;
  2.           f1 = fib(f1, f0);
  3.           cout << "Sequence " << counter + 2<< ": " << f1 << endl;
  4.           counter++;
  5.      }
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....
Mar 7 '08 #2
whodgson
542 512MB
Your while condition restricts to 46
Mar 7 '08 #3
MACKTEK
40
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
Mar 7 '08 #4
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?
Mar 7 '08 #5
MACKTEK
40
Check out "long long" and maybe "unsigned long long"

My guess is those are supported.
Mar 7 '08 #6

Sign in to post your reply or Sign up for a free account.

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,...
62
by: jugaaru | last post by:
How to generate fibonacci mubers in C ?
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: ayman723 | last post by:
hello everyone; I have made this code # include < iostream > # include < iomanip> unsigned long factorial ( unsigned long ); int main ()
3
by: veeru | last post by:
Hi All, Can anyone tell about how to create a FIBONACCI series in VB.Net and C# Thanks in Advance, Veeru
12
by: Santosh Krisnan | last post by:
hello all, I fiddled with BASIC in the early 90s but left it at that. Now I am trying to learn C. I tried to solve an exercise in my book, but it failes to compile. Can anyone tell me what the...
6
by: Andrew Tatum | last post by:
I'm having some problems with the below equation. I have no problems when it comes to positives. Negatives create the problem.. C 2 1 4 However, this doesn't work:
1
by: altaey | last post by:
Question Details: Write a program to find and print a Fibonacci sequence of numbers. The Fibonacci sequence is defined as follow: Fn = Fn-2 + Fn-1, n >= 0 F0 = 0, F1 = 1, F2 = 1 Your...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.