Skip to content

Instantly share code, notes, and snippets.

@bshlgrs
Created November 20, 2013 03:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bshlgrs/7557409 to your computer and use it in GitHub Desktop.
Save bshlgrs/7557409 to your computer and use it in GitHub Desktop.
# 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