Edward Faulkner
12/14/2005 7:58:00 PM
On Thu, Dec 15, 2005 at 04:27:08AM +0900, Christian Neukirchen wrote:
> def fib(n)
> (1..n-2).inject([1, 1]) { |(a, b), n| [b, a+b] }.last
> end
If we're talking about elegance, I prefer this one. It reads just
like the definition of the sequence:
def fib(n)
n > 1 ? fib(n-1) + fib(n-2) : n
end
You can even make it run efficiently by memoizing it. :-)
-Ed