Skip to content

Instantly share code, notes, and snippets.

@lucasmarqs
Created April 1, 2017 22:58
Show Gist options
  • Save lucasmarqs/65d6ca90bb557cdb0c011927b30f0785 to your computer and use it in GitHub Desktop.
Save lucasmarqs/65d6ca90bb557cdb0c011927b30f0785 to your computer and use it in GitHub Desktop.
Recursive Fibonacci with Ruby Hash
fib = Hash.new do |hash, key|
if key < 2
hash[key] = key
else
hash[key] = hash[key-2] + hash[key-1]
end
end
#=> {}
fib[3]
#=> 2
fib
#=> {1=>1, 0=>0, 2=>1, 3=>2}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment