Finding Nth Fibonacci number in constant time.

Rajrishi Vishwakarma - Sep 12 '20 - - Dev Community

The below code uses Binet's Algorithm to find the nth Fibonacci number.

int nthFibonacci(int n){
    double phi= double(1+sqrt(5))/2; 
    return round(pow(phi,n)/sqrt(5));
}
Enter fullscreen mode Exit fullscreen mode
. . .