<script>
// 1,1,2,3,5,8,13,21,34,...
//
function fib(num){
if(num == 1 || num == 2) {
return 1;
}
// alert(fib(num-1))
// alert(fib(num-2))
return fib(num-1) + fib(num-2);
//fib(n) = f(n - 1) + f(n - 2)
}
function fib_add(num){
if(num == 1){
return fib(1);
}
return fib_add(num - 1) +fib(num);
}
alert(fib(5));
alert(fib_add(5))
斐波切纳连求和【递归+递归】
最新推荐文章于 2023-10-04 23:47:51 发布