JS实现~~斐波那数列

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        //1,1,2,3,5,8,13        //输出的结果
        //1,2,3,4,5,6,7         //输入的数字
        /*
            7 = fn(6) + fn(5)  ~ 8 + 5  =13
            6 = fn(5) + fn(4)  ~ 5 + 3  =8
            5 = fn(4) + fn(3)  ~ 3 + 2  =5
            4 = fn(3) + fn(2)  ~ 2 + 1  =3
            3 = fn(2) + fn(1)  ~ 1 + 1  =2
            2 = fn(1) + fn   直接回退
        */
        function fb(n){
            if(n === 1 || n === 2){
                return 1;
            }
            return fb(n-1)+fb(n-2)
        }
        console.log(fb(5));
    </script>
</body>
</html>

JS实现~~斐波那数列

上一篇:Asp.Net Core Web MVC调用Grpc服务器


下一篇:代码,显示IPhone剩余磁盘空间