2

I'm new to programming and need help in C. I am writing a program to generate a Fibonacci sequence for values with up to 1000 digits.

Here is my code:

#include <stdio.h>

int main(void)
{
    int seq[1000];
    int i,n;

    printf("How many Fibonacci numbers do you want?: ");
    scanf("%d",&n);

    seq[0] = 0;
    seq[1] = 1;

    for(i = 2; i < n; i++)
        seq[i] = seq[i-1] + seq[i-2];

    for (i = 1; i < n; i++)
        printf("%d: %d\n", i, seq[i]);

    return 0;
}

Now the problem is, the numbers are all correct up until the 47th number. Then it just goes crazy and there's negative numbers and its all wrong. Can anyone see the error in my code? Any help is greatly appreciated.

GingerPlusPlus
  • 5,336
  • 1
  • 29
  • 52
user3277335
  • 1,947
  • 2
  • 15
  • 16
  • 4
    Have you heard of integer overflow? :) A google search will help. – bbill Feb 05 '14 at 22:37
  • See http://stackoverflow.com/questions/12413294/dynamic-programming-issue-fibonacci-sequence – Joseph Quinsey Feb 05 '14 at 23:20
  • Adding to the information in the answers: The sizes of `int` and `long int` vary from one system to another. The C standard only requires `int` to be at least 16 bits and `long` to be at least 32. *Typically* `int` is 32 bits and `long` is either 32 or 64. `long long` must be at least 64 bits, and is typically exactly 64 bits. The behavior of signed integer overflow is *undefined*. The usual 2's-complement wraparound is typical, but is not guaranteed even for systems that use a 2's-complement representation -- and optimizing compilers can do surprising things. – Keith Thompson Feb 05 '14 at 23:22

3 Answers3

4

I am writing a program to generate a Fibonacci sequence for values with up to 1000 digits.

Not yet you aren't. You are storing the values in variables of type int. Commonly such variables are 32 bit values and have a maximum possible value of 2^31 - 1. That equals 2,147,483,647 which is some way short of your goal of reaching 1,000 digits.

The 47th Fibonacci number is the first number to exceed 2,147,483,647. According to Wolfram Alpha, the value is 2,971,215,073.

When your program attempts to calculate such a number it suffers from integer overflow, because the true value cannot be stored in an int. You could try to analyse exactly what happens when you overflow, why you see negative values, but it really doesn't get you very far. Simply put, what you are attempting is clearly impossible with int.

In order to reach 1,000 digits you need to use a big integer type. None of the built-in types can handle numbers as large as you intend to handle.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I declared the 'seq' array to be of type double. But now the numbers are still wrong from the 37th value. I get 24175186 while it should end in 17, not 16. From there on in there are all the numbers seem t be wrong. Surely its not overflow anymore, type double is a huge value. – user3277335 Feb 05 '14 at 22:58
  • @user3277335 A double holds 15-17 significant decimal digits. That's a lot less than 1,000. You will lose precision, and eventually you will overflow. Again, you cannot fit the values in a double. Think about it. A double is 8 bytes wide. How can you expect to fit 1,000 decimal digits into such a variable. You need a big integer type. You will have to accept the truth of my final paragraph. – David Heffernan Feb 05 '14 at 23:00
  • We were not taught any types other than char, int, double and float. Any clues as to what I could use? – user3277335 Feb 05 '14 at 23:08
  • Yes. Use a bigint type. For instance libgmp – David Heffernan Feb 05 '14 at 23:09
2

The comment I posted above has the simple answer, but here's a more complete version: C often represents integers with a sequence of 32 bits, and the range of values they can take on are from -2,147,483,648 to 2,147,483,647.

Notice what the 47th Fibonacci number is? 2,971,215,073

After they overflow, they wrap around to the smallest integer possible; see 2's complement notation for more information!

For a solution, I might suggest a BigInteger structure. But Fibonacci numbers get huge really fast, so I'm not sure you'd really want to calculate that many.

bbill
  • 2,264
  • 1
  • 22
  • 28
  • Sorry, where did I mention something C-specific? I didn't cite a source for my comments on C#, but here it is: http://msdn.microsoft.com/en-us/library/5kzh1b5w.aspx -EDIT- Oops, thanks! Hasty reading makes for mistakes like that; I'll fix my answer – bbill Feb 05 '14 at 22:44
  • 1
    @bbil If you want upvotes from the asker you have to give him > 15 rep. Pointless asking a rep 1 user for up votes. – David Heffernan Feb 05 '14 at 23:10
  • Thanks, David! Didn't know that, but that makes sense. – bbill Feb 06 '14 at 07:23
2

you are not using the correct data type; fibonacci numbers tend to grow really fast. So you probably are going beyond the limit for int: 2^31. Since int and long are both 32 bit integers (in most cases ->gcc and VS) try using long long .

Pandrei
  • 4,843
  • 3
  • 27
  • 44