如何防止我的斐波那契生成器产生两个 “1”?

huangapple go评论66阅读模式
英文:

Javascript: How do I prevent my Fibonacci generator from producing two "1"?

问题

以下是翻译好的部分:

let sayi3 = 0, sayi4 = 1, nextTerm;
let number = 10;

console.log("Girdiğiniz sayının sırasına kadar olan Fibonacci değerleri:");

for (let i = 1; i <= number; i++) {
    console.log(sayi3);
    nextTerm = sayi3 + sayi4;
    sayi3 = sayi4;
    sayi4 = nextTerm;
}

这是代码的输出:

如何防止我的斐波那契生成器产生两个 “1”?

我的代码没有问题,我只想知道如何阻止它生成两个1。如果可能的话,你能告诉我解决方法吗?

我在论坛和Stack Overflow 上搜索过,但没有结果。

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let sayi3 = 0, sayi4 = 1, nextTerm;
let number = 10;

console.log(&quot;Girdiğiniz sayının sırasına kadar olan Fibonacci değerleri:&quot;);

for (let i = 1; i &lt;= number; i++) {
    console.log(sayi3);
    nextTerm = sayi3 + sayi4;
    sayi3 = sayi4;
    sayi4 = nextTerm;
}

<!-- end snippet -->

This is the output:

如何防止我的斐波那契生成器产生两个 “1”?

There is nothing wrong with my code, I just want to know how can I stop it from generating two 1's. Can you tell me the solution if possible?

I've searched forums and stackoverflow but no results

答案1

得分: -2

这是Fibonacci数列

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 ...

如果你只想要一个 1,那就不再被称为Fibonacci数列了,然而,你可以这样做:

let sayi3 = 1;
let sayi4 = 2;
let nextTerm;
let number = 10;
console.log(0)
for (let i = 2; i <= number; i++) {
    console.log(sayi3);
    nextTerm = sayi3 + sayi4;
    sayi3 = sayi4;
    sayi4 = nextTerm;
}
英文:

This is Fibonacci sequence:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 ...

If you want only one 1, that's not called Fibonacci sequence anymore, however, you can do it this way:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let sayi3 = 1;
let sayi4 = 2;
let nextTerm;
let number = 10;
console.log(0)
for (let i = 2; i &lt;= number; i++) {
    console.log(sayi3);
    nextTerm = sayi3 + sayi4;
    sayi3 = sayi4;
    sayi4 = nextTerm;
}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月17日 20:00:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76704246.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定