Why is this blowing the stack, thought it was tail-recursive...
Scott David Daniels
Scott.Daniels at Acm.Org
Sat Jul 12 15:20:33 EDT 2008
ssecorp wrote:
<a recursive Fibonacci generator thast demonstrates no tail recursion
used>
> and can memoization speed up this even more? ....
Generators get you to an even clearer Fibonacci expression.
def _Fibonacci_gen():
a, b = 1, 0
while True:
a += b
yield a
b += a
yield b
Here's how to use it to avoid redundant comutation:
_next_entry = _Fibonacci_gen().next
_sofar = []
def fib(n):
while len(_sofar) <= n:
_sofar.append(_next_entry())
return _sofar[n]
--Scott David Daniels
Scott.Daniels at Acm.Org
More information about the Python-list
mailing list