This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,13 @@
# Use the Hash#default_proc feature to
# lazily calculate the Fibonacci numbers.
fib = Hash.new do |f, n|
f[n] = if n <= -2
(-1)**(n+1) * f[n.abs]
elsif n <= 1
n.abs
else
f[n-1] + f[n-2]
end
end
# examples: fib[10] => 55, fib[-10] => (-55/1)