ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
### 斐波那契数列 >斐波那契数列:1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...前两位均为1,后面每一位等于前两位的和,求第n位的值,或者前n位的和 1. 递归 ``` function fib(n){ if(n==1 || n==2){ return 1; } return fib(n-1) + fib(n-2); } // console.log(fib(3)); for(let i=1; i<=10; i++){ console.log(fib(i)); } ``` 2. 迭代法 ``` function getNthFibonacci(count) { if(count<0) return 0; if(count<=1) return 1; var first = 1; var second = 1; var third = 0; for(var i = 2; i <= count; i++) { third = first + second; first = second; second = third; } return third; } ```