The Fibonacci algorithm is a one that returns the Fibonacci number. The implementation is usually done via recursion.

Pseudocode

Naïve Recursive:

Algorithm fibonacci(n):
	if n <= 1 then:
		Return n
	End if
	Return fibonacci(n-1) + fibonacci(n-2)
End Algorithm

Dynamic Programming

F ← Array of length n-1
Algorithm fibDP(n):
	if n <= 1 then:
		F[n] = n 
	if F[n] is NOT empty then:
		Return F[n]
	F[n] ← fibDP(n-1) + fibDP(n-2) #Memoization
	Return F[n]

Time Complexity

Naive Implementation where and Here, we know that , so we change the relation to: Dropping all constants… and we know that T(1) is constant time