Python vs Ruby
Greg Jorgensen
gregj at pobox.com
Sat Jan 27 23:03:06 EST 2001
In article <94voh3$5nm$1 at snipp.uninett.no>, "Magnus Lie Hetland"
<mlh at idi.ntnu.no> took Yukihiro Matsumoto to task for his comparison of
Fibonacci functions in Python and Ruby:
> > double? Fibonacci is mostly based on function calls
>
> Not necessarily...
>
> (By the way, your function wasn't altogether correct...)
With all respect, yours is even worse. It doesn't generate a correct
Fibonacci sequence (your function gives 1,1,3,4,5,7,9,12,16,...). A
simpler (and correct) iterative Python function is:
----
def fib(n):
a,b = 0,1
while n > 0:
a,b = b,a+b
n = n-1
return a
----
My version will correctly return 0 for fib(0); your function returns 1.
And I avoid the range() call.
I tested both versions with:
for i in range(10):
print fib(i)
--
Greg Jorgensen
Portland, Oregon, USA
gregj at pobox.com
Sent via Deja.com
http://www.deja.com/
More information about the Python-list
mailing list