<html> <head> <meta charset="utf-8" /> <title>html</title> <script type="text/javascript"> function getNthFibonacci(num){ if(num < 2){ return num; } var first = 0; var second = 1; var third; for(var i = 2;i < num;i ++){ third = first + second; first = second; second = third; } return third; } function output(){ var number = 10; var answer = getNthFibonacci(number); document.write(answer); } </script> </head> <body> </body> <script type="text/javascript">output()</script> </html>
建议不适用递归调用,当数字很大时,递归会非常耗时。
时间: 2024-11-02 21:34:20