Created
March 22, 2016 21:39
-
-
Save Kallin/e6d966e5311afd69de50 to your computer and use it in GitHub Desktop.
adnan's a fibber
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
@fibs = { | |
0 => 0, | |
1 => 1, | |
} | |
def fib(n) | |
cached_result = @fibs[n] | |
return cached_result if cached_result | |
new_result = fib(n-1) + fib(n-2) | |
@fibs[n] = new_result | |
new_result | |
end | |
fib(100) | |
@fibs.values.each { |n| puts n } |
thisduck
commented
Mar 22, 2016
clever, but why result.first(n)
and not just result
?
@Kallin first(n)
because if n = 0 or n = 1 you don't want to just return result = [0,1] because that's the first two numbers
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment