|
'漫谈递归:从斐波那契开始了解尾递归 -- 简明现代魔法
'http://www.nowamagic.net/librarys/veda/detail/2325
Sub main()
Dim t, n
n = 34
t = Timer: Debug.Print fibonacci(n), Timer - t & "s"
t = Timer: Debug.Print fibonacci_tail(n, 1, 1), Timer - t & "s"
End Sub
'
Function fibonacci(n)
If n <= 2 Then
fibonacci = 1
Else
fibonacci = fibonacci(n - 1) + fibonacci(n - 2)
End If
End Function
'
Function fibonacci_tail(n, acc1, acc2)
If n < 2 Then
fibonacci_tail = acc1
Else
Debug.Print n - 1, acc2, acc1 + acc2
fibonacci_tail = fibonacci_tail(n - 1, acc2, acc1 + acc2)
End If
End Function
dg.rar
(29.74 KB, 下载次数: 48)
工作表中的内容仅为帮助自己理解弄的,不知注释的对么?
主要想请教如题,谢谢!
|
|