Recursion: Fibonacci Numbers

  • + 2 comments

    How, without memoization or special formulas? To elaborate a bit more, here is a list of the different methods I can think of for computing the Fibonacci numbers:

    1. Binet's formula
    2. Iterative solution without memoization
    3. Iterative solution with memoization
    4. Recursive solution without memoization
    5. Recursive solution with memoization
    6. Recursive solution using formulas for F(2n) and F(2n+1) with or without memoization.

    If you only plan to call the function a few times and you're not worried about possible floating point errors, then 1 is the best. If you're calling it many times, then 3 is better than 5 is better than 1. If you need to calculate really large Fibonacci numbers, then 6 is the best. Without memoization the performance of solution 4 is horrible.