Jun 16, 2009

Fibonacci in Ruby

a method for Fibonacci number:

def fib(n)
if n < 2
1
else
fib(n - 2) + fib(n - 1)
end
end


result:

puts fib(10) #=> 89

No comments: