types - Fibonacci Sequence in C generating negatives? -


i'm new programming , need in c. writing program generate fibonacci sequence values 1000 digits.

here code:

#include <stdio.h>  int main(void) {     int seq[1000];     int i,n;      printf("how many fibonacci numbers want?: ");     scanf("%d",&n);      seq[0] = 0;     seq[1] = 1;      for(i = 2; < n; i++)         seq[i] = seq[i-1] + seq[i-2];      (i = 1; < n; i++)         printf("%d: %d\n", i, seq[i]);      return 0; } 

now problem is, numbers correct until 47th number. goes crazy , there's negative numbers , wrong. can see error in code? appreciated.

i writing program generate fibonacci sequence values 1000 digits.

not yet aren't. storing values in variables of type int. commonly such variables 32 bit values , have maximum possible value of 2^31 - 1. equals 2,147,483,647 way short of goal of reaching 1,000 digits.

the 47th fibonacci number first number exceed 2,147,483,647. according wolfram alpha, value 2,971,215,073.

when program attempts calculate such number suffers integer overflow, because true value cannot stored in int. try analyse happens when overflow, why see negative values, doesn't far. put, attempting impossible int.

in order reach 1,000 digits need use big integer type. none of built-in types can handle numbers large intend handle.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -