So I have the code
#include <iostream>
using namespace::std;
int main() {
int a = 1;
int b = 1;
while (a < 100) {
a = a + b;
b = a + b;
cout << a << endl;
cout << b << endl;
}
}
What it does is print the fibonacci sequence up to 100. But, when I make “a” bigger, say, (OVER 9000!!!) 10 billion, it just prints out random numbers it seems. Why does it do this?
This probably happens when you exceed the range of an integer (probably 2,147,483,647 on Windows 32-bit). Try changing the types to
long longand see if you get more accurate results to a larger range. Alternatively, you could change addunsignedto the type because the fib. sequence is strictly positive, so that’ll double the range of values.This is architecture dependent, but using ranges from http://msdn.microsoft.com/en-us/library/s3f49ktz(v=vs.80).aspx, your ranges would be:
Edit: totally off-topic, but thanks for putting me over 1k you glorious voting bastards!