Fibonacci: How to think recursively

Mike hennebry at web.cs.ndsu.nodak.edu
Wed Sep 1 19:31:27 CEST 2010


The most straightforward method would be to apply the formula
directly.
Loop on j computing Fj along the way
if n<=1 : return n

Fold=0
Fnew=1
for j in range(2,n) :
    Fold, Fnew = Fnew, Fold+Fnew
return Fnew

Even simpler:
return round(((1+sqrt(5.))/2)**n/sqrt(5.))



More information about the Python-list mailing list