0

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++?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Side note: `#include` should be `#include` but from what I can see, you don't need that file at all. – Ted Lyngmo Jun 11 '20 at 12:05
  • 2
    hint: what is the last digit of `123123123123....1 million more digits....3 + 43534534534... 42 million more digits....1` ? – 463035818_is_not_an_ai Jun 11 '20 at 12:06
  • @Nurnabi Haider Why are you trying to output a whole fibonacci number instead of its last digit? – Vlad from Moscow Jun 11 '20 at 12:08
  • All built-in integral types have finite upper and lower limits on the value they can represent. There is no unbounded type. If you want an unbounded integral type (at least, within limits of available memory) you need to either obtain a library that supports such things, or implement your own. – Peter Jun 11 '20 at 12:09
  • spoiler: the answer is the same for `3 + 1` – 463035818_is_not_an_ai Jun 11 '20 at 12:09
  • The 239:th is `39679027332006820581608740953902289877834488152161` and the largest integer you'll probably be able to handle is `18446744073709551616`. – Ted Lyngmo Jun 11 '20 at 12:10
  • You just need to store the last digit, for some other purpose if you need to store numbers greater than 10^18 use string. – Tanya Jun 11 '20 at 12:26

2 Answers2

1

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
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

The built-in integer data types in C++ can only store values in a given range. For int64 that would be -263 to 263-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 (i.e. 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.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
  • when i do return (b%10) then it shows the answer of -1..so its actually taking the reminder of the negative value from the other end.what shall i do then? – Nurnabi Haider Jun 11 '20 at 19:17
  • You should take the remainder at each step, not only at the end. If you do that you will never get to negatives, the value will always stay in the range [0, 9] – Ivaylo Strandjev Jun 12 '20 at 09:48