Created
November 20, 2013 03:50
-
-
Save bshlgrs/7557409 to your computer and use it in GitHub Desktop.
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
# Memoized Fibonacci Sequence | |
# The first line here initializes a hash map. | |
# The bit in the curly braces is a code block to be evaluated as the default value | |
# when the hash map doesn't have a key in it. | |
f = Hash.new {|hash,key| hash[key] = hash[key-1] + hash[key-2] } | |
# To give it a base case, we just give the dictionary particular values. | |
f[0] = 0 | |
f[1] = 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment