Fibonacci sequence by using binets formula

·

1 min read

By using binet's theorem we can solve this in O(1) time complexity & O(1) space complexity.

#include

#include

int fib(int n){ double sqrt5 = sqrt(5); int F_n = ( pow((1 + sqrt5), n) - pow((1 - sqrt5), n) ) / ( pow(2, n) * sqrt5 ); return F_n; }