var x = 0;
var y = 1;
var z;
fib[0] = 0;
fib[1] = 1;
for (i = 2; i <= 10; i++) {
alert(x + y);
fib[i] = x + y;
x = y;
z = y;
}
我尝试生成一个简单的斐波那契数列,但没有输出。
请问有人能告诉我出了什么问题吗?
var x = 0;
var y = 1;
var z;
fib[0] = 0;
fib[1] = 1;
for (i = 2; i <= 10; i++) {
alert(x + y);
fib[i] = x + y;
x = y;
z = y;
}
我尝试生成一个简单的斐波那契数列,但没有输出。
请问有人能告诉我出了什么问题吗?
function fibonacciRecursive(a, b, counter, len) {
if (counter <= len) {
console.log(a);
fibonacciRecursive(b, a + b, counter + 1, len);
}
}
fibonacciRecursive(0, 1, 1, 20);
结果
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
function fibo(count) {
//when count is 0, just return
if (!count) return;
//Push 0 as the first element into an array
var fibArr = [0];
//when count is 1, just print and return
if (count === 1) {
console.log(fibArr);
return;
}
//Now push 1 as the next element to the same array
fibArr.push(1);
//Start the iteration from 2 to the count
for(var i = 2, len = count; i < len; i++) {
//Addition of previous and one before previous
fibArr.push(fibArr[i-1] + fibArr[i-2]);
}
//outputs the final fibonacci series
console.log(fibArr);
}
fibo(20); //output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181]
function fibonacci (n, length) {
if (n < 2) {
return [1];
}
if (n < 3) {
return [1, 1];
}
let a = fibonacci(n - 1);
a.push(a[n - 2] + a[n - 3]);
return (a.length === length)
? a.map(val => console.log(val))
: a;
};
fibonacci(5, 5)
的输出结果将是:
1
1
2
3
5
变量a
的赋值是斐波那契
函数的返回值。在下一行,计算出斐波那契数列的下一个值,并将其推到a
数组的末尾。
斐波那契
函数的length
参数用于比较a
数组的序列长度,必须与n
参数相同。当序列的长度与长度参数匹配时,将a
数组输出到控制台,否则函数将返回a
数组并重复执行。
ES6 - Symbol.iterator 和 generator 函数:
let fibonacci = {
*[Symbol.iterator]() {
let pre = 0, cur = 1
for (;;) {
[ pre, cur ] = [ cur, pre + cur ]
yield cur
}
}
}
for (let n of fibonacci) {
if (n > 1000)
break
console.log(n)
}
最简单的一个
var output = [0,1];
for (let i = 2; i < 10; i++) {
output.push(output[i-2] + output[i-1])
}
console.log(output)
let maxNum = 10; // can change as per your desired length
const fibonnaci = (terms) => {
let series = [0, 1], a = 1, b = 0, f = 0;
for (let i = 0; i < terms; b = a, a = f, i++) {
f = b + a
series.push(f)
}
console.log(series) // [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …]
}
fibonnaci(maxNum)
干杯!
<!DOCTYPE html>
<html>
<body>
<p id="fibonacci">Fibonacci</p>
<script>
var fibo = fibonacci()
function* fibonacci() {
var x = 1, y = 1, z = 0
yield* [x, y];
while(true) {
z = x + y, x = y, y = z;
yield z;
}
}
setInterval(
() => document.getElementById("fibonacci").innerHTML = fibo.next().value
, 1000);
</script>
</body>
</html>
我的看法:
function fibonacciGenerator( n = 0 )
{
if (n<0) throw 'Fibonacci generator accept only positive value'
let arr = [0,1,1];
if (n < 2) arr.splice( n+1, 2-n)
else for (let i=2; i<n; ++i) arr.push( arr[i] + arr[i-1] )
return arr.join('-')
}
console.log( '0 ->', fibonacciGenerator(0) )
console.log( '1 ->', fibonacciGenerator(1) )
console.log( '2 ->', fibonacciGenerator(2) )
console.log( '3 ->', fibonacciGenerator(3) )
console.log( '12 ->', fibonacciGenerator(12) )
console.log( '-8 ->' )
console.log( fibonacciGenerator(-8) )
.as-console-wrapper {max-height: 100% !important;top: 0;}
.as-console-row::after {display: none !important;}
function fibo(n, prevValues = []) {
if (prevValues[n] != null) {
return prevValues[n];
}
let result;
if (n <= 2) {
result = 1
} else {
result = fibo(n - 1, prevValues) + fibo(n - 2, prevValues);
}
prevValues[n] = result;
return result;
}
console.log(fibo(41))
var a = 0;
console.log(a);
var b = 1;
console.log(b);
var c;
for (i = 0; i < 3; i++) {
c = a + b;
console.log(c);
a = b + c;
console.log(a);
b = c + a;
console.log(b);
}