递归函数的调用次数计算

递归函数的调用次数计算

def fibonacci(n):
	if n <= 1:
		return n
	else:
		return fibonacci(n-2) + fibonacci(n-1)

f i b o n a c c i ( 7 ) fibonacci(7) fibonacci(7)调用次数计算过程:
C0 = 1
C1 = 1
C2 = 1+ C0 + C1 = 1 + 1 + 1 = 3
C3 = 1+ C1 + C2 = 1 + 1 + 3 = 5
C4 = 1+ C2 + C3 = 1 + 3 + 5 = 9
C5 = 1+ C3 + C4 = 1 + 5 + 9 = 15
C6 = 1+ C4 + C5 = 1 + 9 + 15 = 25
C7 = 1+ C5 + C6 = 1 + 15 + 25 = 41

你可能感兴趣的:(个人)