看了python 官网的生成斐波那契数列 示例,于是想使用JavaScript 试试。
python
生成斐波那契数列
# Python 3: Fibonacci series up to n
>>> def fib(n):
>>> a, b = 0, 1
>>> while a < n:
>>> print(a, end=' ')
>>> a, b = b, a+b
>>> print()
>>> fib(1000)
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
python中a, b = a, a + b这条语句是如何执行的?
javascript
生成斐波那契数列, 5000 以内
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181]
let a = 1;
let b = 1;
let arr = [];
while(a<5000){
arr.push(a);
arr.push(b);
a = a+b;
b = a+b;
};
console.log(arr);
javascript
生成斐波那契数列,第 100 个值是什么
function getFbli(n){
let a = 1;
let b = 1;
let i = 1;
while(i<=n){
if(i==n){
console.log(a);
}
i = i+1;
if(i==n){
console.log(b);
}
i = i+1;
a = a+b;
b = a+b;
};
};
getFbli(100) // 354224848179262000000