print fibo big numbers in c++ or c language

后端 未结 6 1226
夕颜
夕颜 2021-01-16 23:39

I write this code for show fibonacci series using recursion.But It not show correctly for n>43 (ex: for n=100 show:-980107325).

#include
#inc         


        
相关标签:
6条回答
  • 2021-01-16 23:57

    For this purporse you need implement BigInteger. There is no such build-in support in current c++. You can view few advises on stack overflow

    Or you also can use some libs like GMP

    Also here is some implementation:

    1. E-maxx - on Russian language description.

    2. Or find some open implementation on GitHub

    0 讨论(0)
  • 2021-01-16 23:58

    Keep in mind that the n-th fibonacci number is (approximately) ((1 + sqrt(5))/2)^n.

    This allows you to get the value for n that allows the result to fit in 32 /64 unsigned integers. For signed remember that you lose one bit.

    0 讨论(0)
  • 2021-01-17 00:02

    Code overflows the range of the integer used long.
    Could use long long, but even that may not handle Fib(100) which needs at least 69 bits.

    Code could use long double if 1.0/LDBL_EPSILON > 3.6e20

    Various libraries exist to handle very large integers.

    For this task, all that is needed is a way to add two large integers. Consider using a string. An inefficient but simply string addition follows. No contingencies for buffer overflow.

    #include <stdio.h>
    #include <string.h>
    #include <assert.h>
    
    char *str_revese_inplace(char *s) {
      char *left = s;
      char *right = s + strlen(s);
      while (right > left) {
        right--;
        char t = *right;
        *right = *left;
        *left = t;
        left++;
      }
      return s;
    }
    
    char *str_add(char *ssum, const char *sa, const char *sb) {
      const char *pa = sa + strlen(sa);
      const char *pb = sb + strlen(sb);
      char *psum = ssum;
      int carry = 0;
      while (pa > sa || pb > sb || carry) {
        int sum = carry;
        if (pa > sa) sum += *(--pa) - '0';
        if (pb > sb) sum += *(--pb) - '0';
        *psum++ = sum % 10 + '0';
        carry = sum / 10;
      }
      *psum = '\0';
      return str_revese_inplace(ssum);
    }
    
    int main(void) {
      char fib[3][300];
      strcpy(fib[0], "0");
      strcpy(fib[1], "1");
      int i;
      for (i = 2; i <= 1000; i++) {
        printf("Fib(%3d) %s.\n", i, str_add(fib[2], fib[1], fib[0]));
        strcpy(fib[0], fib[1]);
        strcpy(fib[1], fib[2]);
      }
      return 0;
    }
    

    Output

    Fib(  2) 1.
    Fib(  3) 2.
    Fib(  4) 3.
    Fib(  5) 5.
    Fib(  6) 8.
    ...
    Fib(100) 3542248xxxxxxxxxx5075.  // Some xx left in for a bit of mystery.
    
    Fib(1000) --> 43466...about 200 more digits...8875
    
    0 讨论(0)
  • 2021-01-17 00:04

    Try to use a different format and printf, use unsigned to get wider range of digits.

    If you use unsigned long long you should get until 18 446 744 073 709 551 615 so until the 93th number for fibonacci serie 12200160415121876738 but after this one you will get incorrect result because the 94th number 19740274219868223167 is too big for unsigned long long.

    0 讨论(0)
  • 2021-01-17 00:14

    Well, you could want to try implementing BigInt in C++ or C.

    Useful Material:

    • How to implement big int in C++
    0 讨论(0)
  • 2021-01-17 00:17

    The value of fib(100) is so large that it will overflow even a 64 bit number. To operate on such large values, you need to do arbitrary-precision arithmetic. Arbitrary-precision arithmetic is not provided by C nor C++ standard libraries, so you'll need to either implement it yourself or use a library written by someone else.

    For smaller values that do fit your long long, your problem is that you use the wrong printf format specifier. To print a long long, you need to use %lld.

    0 讨论(0)
提交回复
热议问题