Popular Posts

Monday, July 18, 2011

nth Fibonacci number

Problem:
Write a method to generate the nth Fibonacci number.
Solution:
There are three potential approaches: (1) recursive approach (2) iterative approach (3) using matrix math. We have described the recursive and iterative approach below, as you would not be expected to be able to derive the matrix-based approach in an interview. For the interested math-geeks, you may read about the (most efficient) matrix-based algorithm at http://en.wikipedia.org/wiki/Fibonacci_number#Matrix_form.
Complexity: 
Code:

Recursive Solution:


Iterative Solution:

No comments:

Post a Comment