简体   繁体   中英

Program Last Digit of a Large Fibonacci Number for n-th Fibonacci number. C++

I'm trying to solve Fibonacci using C++, but my code shows negative numbers when the output digit limit crosses with big numbers.

 #include<stdio.h>
 #include<iostream>
 using namespace std;

int64_t get_fibonacci_last_digit_naive(int n)
    {
    int64_t a = 0, c, i;
    int64_t b = 1;



    if (n == 0)
       return (a);
     for (i = 2; i <= n; i++)
      {
         c = a + b;
         a = b;
         b = c;


      }
      return (b);
      }

   int main()
     {
    int n;

     cin >> n;

     cout << get_fibonacci_last_digit_naive(n) << '\n';

     return 0;
    }

so when my input is 239, my out put shows

    239
    -1716907696316940191

    Process returned 0 (0x0)   execution time : 12.395 s
    Press any key to continue.

Now how i can store digits with no upper limits in C++?. i am very noob and exlpain me thinking that i dont know anythg. so huw can i print digits more than 30 in c++?

For starters pay attention to that this header

#include<stdio.h>

is redundant. Neither declaration from the header is used in your program. And in C++ you should at least specify

#include <cstdio> 

if a declaration from the header is required.

To get such a big fibonacci number as the 239-th fibonacci number you need to use a special library that provides services for processing big numbers or you have to write such services yourself by using for example the standard class std::string .

However to get the last digit of a fibonacci number there is no need to calculate the whole fibonacci number. It is enough to track only last digits.

Here is my naive approach.:)

#include <iostream>
#include <functional>

unsigned int get_fibonacci_last_digit_my_naive( unsigned int n )
{
    const unsigned int Base = 10;

    unsigned int a[] = { 0, 1 };

    while (n--)
    {
        a[1] += std::exchange( a[0], a[1] );
        a[1] %= Base;
    }

    return a[0];
}

int main() 
{
    unsigned int n = 0;

    std::cin >> n;

    std::cout << "The last digit of the " << n << "-th fibonacci number is "
              << get_fibonacci_last_digit_my_naive( n ) << '\n';

    return 0;
}

The program output is

The last digit of the 239-th fibonacci number is 1

Or if to change the function main the following way

int main() 
{
    unsigned int n = 0;

    std::cin >> n;

    for ( unsigned int i = n; i < n + 10; i++ )
    {
        std::cout << "The last digit of the " << i << "-th fibonacci number is "
                  << get_fibonacci_last_digit_my_naive( i ) << '\n';
    }                 

    return 0;
} 

and to enter the number 230 then the program output will be

The last digit of the 230-th fibonacci number is 5
The last digit of the 231-th fibonacci number is 4
The last digit of the 232-th fibonacci number is 9
The last digit of the 233-th fibonacci number is 3
The last digit of the 234-th fibonacci number is 2
The last digit of the 235-th fibonacci number is 5
The last digit of the 236-th fibonacci number is 7
The last digit of the 237-th fibonacci number is 2
The last digit of the 238-th fibonacci number is 9
The last digit of the 239-th fibonacci number is 1

The built-in integer data types in C++ can only store values in a given range. For int64 that would be -2 63 to 2 63 -1 (NOTE: prior to C++20 the standard allows for different signed integer representations so in theory the limits listed may differ by +/- 1). If a calculation results in values outside of this range you will get integer over flow and your value will continue from the other end of the range. This is the reason you see negative values - the 239-th Fibonacci number is actually very big(it has 50 digits in its decimal notation) and can not be stored in any built-in data type.

On the other hand to compute only the last digit of the 239-th Fibonacci number you do not need to actually compute the whole value. In fact for any number in decimal notation its last digit is the remainder of the number when divided by 10 (ie the last digit of X is X%10 ). This also applies for arithmetic operations for instance the last digit of A + B is (A % 10 + B % 10) % 10 . I hope this tip helps you solve the problem on your own.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM