[Tutor] fibonacci

Ike Hall Ike Hall <hall@nhn.ou.edu>
Wed Feb 12 14:45:01 2003


Hi,
yeah, I realized that when after I sent it...dont drink and code...

Ike
----- Original Message -----
From: "Danny Yoo" <dyoo@hkn.eecs.berkeley.edu>
To: "Ike Hall" <hall@nhn.ou.edu>
Cc: "Mic Forster" <micforster@yahoo.com>; <tutor@python.org>
Sent: Wednesday, February 12, 2003 1:30 AM
Subject: Re: [Tutor] fibonacci


>
>
> On Wed, 12 Feb 2003, Ike Hall wrote:
>
> > Actually, the Fibonacci Sequence is quite simple,
> >
> > its 1,1,2,3,5,8,13,....
> >
> > each number is the sum of the previous 2....
> >
> > writing a program to do this would be simple as well...
> >
> > def fib(index):
> >     first=1
> >     second=1
> >     for i in range(index):
> >         new=first+second
> >         first=second
> >         second=new
> >     return new
> >
> > then this function would give the nth number in a fibonacci sequence
>
>
> Are you sure?
>
> This is not a flippant question: there's a subtle off-by-one bug in this
> implementation, so that fib(0) breaks, and every other value is off an
> index.
>
> (In the last statement of the block, I think the code can be repaired by
> returning 'first', rather than 'new'.)
>
>
> Even the simplest of programs need testing.  I've been bitten way too much
> by silly typos to trust myself... *grin*.
>
>
> Good luck!
>