Created
September 30, 2011 19:25
-
-
Save ciscou/1254735 to your computer and use it in GitHub Desktop.
Efficient yet beautiful ruby fibonacci implementation
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
class Fixnum | |
@@fibo_cache = Hash.new { |h, k| h[k] = k < 2 ? k : h[k-2] + h[k-1] } | |
def fibo | |
@@fibo_cache[self] | |
end | |
end | |
puts (0..500).map { |n| "#{n.to_s.rjust(3, ' ')} => #{n.fibo}" } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment