So I have the following code:
#include <stdio.h>
unsigned long int fibonacci()
{
static int count = -1;
static int f1 = 0;
static int f2 = 1;
count++;
if(count == 0 || count == 1) {
return count;
} else {
int f3 = f1 + f2;
f1 = f2;
f2 = f3;
return f3;
}
}
int main()
{
int i;
for(i = 1; i <= 50; i++) {
printf("\n%i: %li", i, fibonacci());
}
return 0;
}
For the 48th fibonacci number, the output is -1323752223 indicating that the initial number was too large for the data type of the variable. The 48th fibonacci number is 2971215073 but google says the data type long int can handle numbers up to over 4 billion. So why can't it handle a number barely under 3 billion?
long intis able to represent numbers this large. That is different from platform to platform.long intcan handle…”: Google cannot tell you what numberslong intcan handle because it is not a fixed limit. It varies by C implementation. In some C implementations, the maximumlong intis 2,147,483,647, in others it is 9,223,372,036,854,775,807, and it could be other values. Do not just Google things and use some part of what you find without understanding it.int.intandlong int(which is the same asinton many platforms) withlong long intor evenunsigned long long intas you don't deal with negative numbers, then it might work. Also read this SO article: stackoverflow.com/q/18971732/898348