0

I have a fibonacci problem where I want to calculate nth fibonacci and want its last digit (digit I get when I'll do it % 10). n would be given and can be up to 10^18.

unsigned long long int nthfib(long long int n) { 
    double phi = (1 + sqrt(5)) / 2; 
    return round(pow(phi, n-1) / sqrt(5));
}

The above code, for large n, e.g. 1024, gives such a big number that I can't store it in a variable and find its % 10.

As time is an issue, I want the solution in O(1).

Cee McSharpface
  • 8,493
  • 3
  • 36
  • 77
  • 2
    Hint: `fib(n) = fib(n-1) + fib(n-2)`, and `(A+B)%10 = A%10 + B%10` – Botje Sep 06 '19 at 11:33
  • `std::round` returns `double`, not `int`. And you can't take modulo of a `double` (at least not in C++). – Yksisarvinen Sep 06 '19 at 11:34
  • Do you need the whole number for anything? If not, do each sum modulo 10, and you'll only have the 'ones' digit to worry about at any point, sidestepping your problem entirely. – LowIntHighCha Sep 06 '19 at 11:41
  • Also you might want to use the classical addition method by storing the digits and implementing addition manually: using the closed form formula causes quick loss of precision if you are using double. – Supersonic Sep 06 '19 at 11:41
  • 1
    Wait a minute: what is the "ones digit"? I thought you meant all digits that were 1, but it seems you mean the digit in the last place. – MSalters Sep 06 '19 at 11:42
  • I don't require the whole number. I just need the **ones** place digit of the nth fib number. – Ansh Shrivastava Sep 06 '19 at 11:47
  • 1
    @AnshShrivastava assume a number would be 213151617 do you need the '2' or the '7' or 4 (the number of digits that are '1') – xception Sep 06 '19 at 11:48
  • for clarification in my example above the `2` would be called the `first digit`, while the `7` would be named the `last digit`, or in more techical terms `2` is the `most significant digit` and `7` is the `least significant digit` – xception Sep 06 '19 at 11:54
  • I want the `last digit` i.e. 7 @xception – Ansh Shrivastava Sep 06 '19 at 11:56
  • I don't understand why the input to binet's formula should be a `long long`. does it not indicate the n, the digit index? – Cee McSharpface Sep 06 '19 at 12:23
  • @dlatikay "n would be given and can be up to 10^18", which is larger than 2^32 – VLL Sep 09 '19 at 05:49

2 Answers2

7

Fibonacci numbers follow a pattern where the last digit repeats every 60 numbers. See http://mathworld.wolfram.com/FibonacciNumber.html

The sequence of final digits in Fibonacci numbers repeats in cycles of 60. The last two digits repeat in 300, the last three in 1500, the last four in 15000, etc. The number of Fibonacci numbers between n and 2n is either 1 or 2 (Wells 1986, p. 65).

Wells, D. The Penguin Dictionary of Curious and Interesting Numbers. Middlesex, England: Penguin Books, pp. 61-67, 1986.

To get the last digit of the nth number in a constant time, you can calculate the last digits of the first 60 numbers into an array and then return the n % 60th digit in that array.

VLL
  • 9,634
  • 1
  • 29
  • 54
3

If you want just the last digit you don't need to store the entire number for it.

a = 0; // return a if n==1
b = 1; // return b if n==2
for(i=2;i<n;i++){
    c = (a+b)%10;
    a=b;
    b=c;
}
return b;

Instead of storing the entire number you are just storing their last digits.

T.Square
  • 201
  • 3
  • 11