From: Christian Neukirchen Date: 2005-12-15T05:27:58+09:00 Subject: Re: ruby beats them all Edward Faulkner writes: > 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 Yes. But Ruby is not tail-recursive (neither is your method), and this solution wont work for bigger values. > You can even make it run efficiently by memoizing it. :-) If it was about efficiency, I'd calculate them with the golden mean... > -Ed -- Christian Neukirchen http://chneukirchen.org