Created
September 28, 2012 05:50
-
-
Save ikataitsev/3798133 to your computer and use it in GitHub Desktop.
Calculating Fibonacci series with dictionary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fib n, dict = [] | |
if n < 2 | |
dict[0] = 1 | |
else | |
dict[n-1] = ( dict[n-2] || fib(n-1, dict).last ) + ( dict[n-3] || fib(n-2, dict).last ) | |
end | |
dict | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is the recursive equation to generate fibonacci series using recursion.
fibonacci(N) = Nth term in fibonacci series
fibonacci(N) = fibonacci(N - 1) + fibonacci(N - 2);
whereas, fibonacci(0) = 0 and fibonacci(1) = 1