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).