Shuvro-d/Dynamic.Programming

Make the fib function faster

Closed this issue · 2 comments

Problem

The fib function time complexity is O(n) but I want to make it O(Log n)

How

Using the Fibonacci nth term formula :

if n is postive:
   (phi^n) / sqrt(5) 
if n is negative
   ((-phi)^(-n)) / sqrt(5)

Complexity

Time

Because of the Pow function the time complexity is Logarithmic -> O(Log n)

Space

Constant -> O(1)

May I make this change?

Thanks for your suggestion. I will update it.