DEV Community

RajRishi Vishwakarma
RajRishi Vishwakarma

Posted on

Finding Nth Fibonacci number in constant time.

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

Top comments (0)