Skip to content

Instantly share code, notes, and snippets.

@ciscou
Created September 30, 2011 19:25
Show Gist options
  • Save ciscou/1254735 to your computer and use it in GitHub Desktop.
Save ciscou/1254735 to your computer and use it in GitHub Desktop.
Efficient yet beautiful ruby fibonacci implementation
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