From: jzakiya Date: 2009-07-13T02:15:06+09:00 Subject: Re: Fibonacci numbers - newbie question. On Jul 12, 5:40 am, Stefano Crocco wrote: > On Sunday 12 July 2009, salai wrote: > > > > > |Dear All, > > | > > |I have two Fibonacci method, and I got different result. > > | > > |What wrong in my code.? > > | > > |def fib(n) > > |  if n < 2 > > |          1 > > |  else > > |          fib(n-2) + fib(n-1) > > |  end > > |end > > | > > |puts fib(10) # ---> 89 > > | > > | > > |def fib1(x) > > |  return x if x < 2 > > |  return fib1(x- 1) + fib1(x - 2) > > |end > > | > > | > > | > > |puts fib1(10) # --> 55 > > | > > | > > |regards, > > |salai. > > In the first case, if the number is less than 2, you always return 1. In the > second case, you return the number itself (that is, 1 if x is 1 and 0 if x is > 0). > > Stefano Many people forget (or don't know) the series starts: Fib(0)=0 Fib(1)=1 Fib(2)=1 .... So when x < 2 then Fib=x These initial conditions are important for doing algebraic forms for the series. And actually, the full series can be extended into negative numbers. See at url: http://en.wikipedia.org/wiki/Fibonacci_number