生成斐波那契数列

48
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;
}

我尝试生成一个简单的斐波那契数列,但没有输出。

请问有人能告诉我出了什么问题吗?

52个回答

1
我想作为答案添加一些代码 :), 编程永远不会太晚 :P
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


1
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方法,并获得斐波那契序列直到计数。
fibo(20); //output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181]

0
这是一个使用递归生成并完整显示斐波那契数列的函数:
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数组并重复执行。


0

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)
}

0

最简单的一个

var output = [0,1];

for (let i = 2; i < 10; i++) {  

 output.push(output[i-2] + output[i-1])  
 
}
console.log(output)

0
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)

干杯!


0
<!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>

0

我的看法:

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;}


0
为了减少时间并优化性能,我们可以在斐波那契数列中使用记忆化技术,因为计算fibo(40)将需要太长时间,为了处理这种情况,记忆化技术应运而生。(记忆化技术基本上是根据输入缓存值的使用,在简单的说法中,我们可以说我们存储先前值的结果)。

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))


0
你可以在这里尝试这个斐波那契数列解法。
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);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接