deffibonacci_fast(n):if n <=1:return n
else:return(fibonacci_fast(n -1)+ fibonacci_fast(n -2))print(fibonacci_fast(10))deffibonacci_fast_2(n):
a, b =0,1for _ inrange(n):
a, b = a + b, a
return a
print(fibonacci_fast_2(10))
尾递归 python2
#!/usr/bin/env python2.4# This program shows off a python decorator(# which implements tail call optimization. It# does this by throwing an exception if it is # it's own grandparent, and catching such # exceptions to recall the stack.import sys
classTailRecurseException:def__init__(self, args, kwargs):
self.args = args
self.kwargs = kwargs
deftail_call_optimized(g):"""
This function decorates a function with tail call
optimization. It does this by throwing an exception
if it is it's own grandparent, and catching such
exceptions to fake the tail call optimization.
This function fails if the decorated
function recurses in a non-tail context.
"""deffunc(*args,**kwargs):
f = sys._getframe()if f.f_back and f.f_back.f_back \
and f.f_back.f_back.f_code == f.f_code:raise TailRecurseException(args, kwargs)else:while1:try:return g(*args,**kwargs)except TailRecurseException, e:
args = e.args
kwargs = e.kwargs
func.__doc__ = g.__doc__
return func
@tail_call_optimized
deffactorial(n, acc=1):"calculate a factorial"if n ==0:return acc
return factorial(n-1, n*acc)print factorial(10000)# prints a big, big number,# but doesn't hit the recursion limit.
@tail_call_optimized
deffib(i, current =0,next=1):if i ==0:return current
else:return fib(i -1,next, current +next)print fib(10000)