def LinearFibonacci(n):
fn = f1 = f2 = 1
for x in xrange(2, n):
fn = f1 + f2
f2, f1 = f1, fn
return fn
fn is not needed here, and if one opts to use it anyway, need not be initialised before the loop. Furthermore, the variable names "f1" and "f2" are confusing here, because I expect f2 to be bigger than f1. I would write this code as:
def LinearFibonacci(n):
curr, next = 1, 1
for x in xrange(2, n):
next, curr = ((next+curr),next)
return next
I am being sponsored by Syntress! They bought me an amazing dedicated server to run catonmat on. If you're looking web services, I highly recommend the Syntress guys!
Hi all!
Just a note about the Python code:
def LinearFibonacci(n): fn = f1 = f2 = 1 for x in xrange(2, n): fn = f1 + f2 f2, f1 = f1, fn return fnfn is not needed here, and if one opts to use it anyway, need not be initialised before the loop. Furthermore, the variable names "f1" and "f2" are confusing here, because I expect f2 to be bigger than f1. I would write this code as:
def LinearFibonacci(n): curr, next = 1, 1 for x in xrange(2, n): next, curr = ((next+curr),next) return nextTested and works.
Reply To This Comment