斐波那契数列与 JavaScript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22909549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fibonacci Series with JavaScript
提问by Gigi
var var1 = 0;
var var2 = 1;
var var3;
var num = 20;
document.write(var1 + "<br />");
document.write(var2 + "<br />");
for (var i = 3; i <= num; i++) {
var3 = var1 + var2;
var1 = var2;
var2 = var3;
document.write(var3 + "<br />");
}
Best, I'm doing a Fibonacci Series, but I want to see a message with prompt comes up, so you can fill in a number, then the returns with the Fibonacci Series. Who would be able to help me? Above I have now.
最好,我正在做斐波那契数列,但我想看到一条带有提示的消息,因此您可以填写一个数字,然后返回斐波那契数列。谁能帮助我?以上我现在有。
回答by Vikram Rajput
回答by CHEUK KEI FU
function myFunction() {
var n = document.getElementById("myNumber").value;
document.getElementById("demo").innerHTML = fibonacciGenerator (n);
}
var sequence = [0]; // sequence = []; if you want sequence [1] = 1;
var previousNumber = 1;
var presentNumber = 0;
var sum = 0;
function fibonacciGenerator (n) {
while (sequence.length < n) {
sum = previousNumber + presentNumber;
previousNumber = presentNumber;
presentNumber = sum;
sequence.push(sum);
}
return (sequence);
}
<!DOCTYPE html>
<html>
<body>
<h1>Fibonacci Generator </h1>
<input type="number" id="myNumber" value="0">
<p>Enter a number to generate Fibonacci array.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
</body>
</html>
回答by Viraj
i came up with this solution to get the n index Fibonacci value. you can use the findFac0();
to pass the index you need to get the Fibonacci value.
我想出了这个解决方案来获得 n 指数斐波那契值。您可以使用findFac0();
传递获取斐波那契值所需的索引。
function findFac(n){
if (n===1)
{
return [0, 1];
}
else
{
var s = findFac(n - 1);
s.push(s[s.length - 1] + s[s.length - 2]);
return s;
}
}
function findFac0(n){
var vv1 = findFac(n);
return vv1[n-1];
}
console.log(findFac0(10));
回答by chris97ong
I hope that this is what you are looking for.
我希望这就是你正在寻找的。
HTML:
HTML:
Which sequence of the Fibonacci pattern do you want to find?
<br><br>
<input type="text" id="inputtext">
<br><br>
<input type="button" value="Find out" id="btn">
<br><br>
<b id="ID"></b>
JS:
JS:
function add(a, b) {
while (a.length < b.length) a.unshift(0);
while (a.length > b.length) b.unshift(0);
var carry = 0,
sum = [];
for (var i = a.length - 1; i >= 0; i--) {
var s = a[i] + b[i] + carry;
if (s >= 10) {
s = s - 10;
carry = 1;
} else {
carry = 0;
}
sum.unshift(s);
}
if (carry) sum.unshift(carry);
return sum;
}
function fib(n) {
var f1 = [0];
var f2 = [1];
while (n--) {
var f3 = add(f1, f2);
f1 = f2;
f2 = f3;
}
return f1.join("");
}
document.getElementById("btn").onclick = function () {
var inputnum = parseFloat(document.getElementById("inputtext").value);
document.getElementById("ID").innerHTML = fib(inputnum).toString();
};
小提琴。
回答by Aidan
you need to use a prompt box to get the number:
您需要使用提示框来获取号码:
window.prompt("sometext","defaultText");
回答by Chirag Bansal
var number = prompt("Enter number ");
....... your code ....
......你的代码......
回答by Aashray
You can do the following:
您可以执行以下操作:
var var1 = 0;
var var2 = 1;
var var3;
var num = window.prompt("Enter the limit for your series:","");
//var num = 20;
var str = '';
str+=var1+','+var2;
for(var i=3; i <= parseInt(num);i++)
{
var3 = var1 + var2;
var1 = var2;
var2 = var3;
str+=','+var3;
}
document.write(str);
回答by vijay_ky
<!doctype html>
<html lang="en">
<head>
<body>
<script type="text/javascript">
var f1=0,f2=1,f3;
var i;
alert("enter a text"+n);
var n=prompt("enter the number");
document.write("the fibonacci series is "+"<br/>");
for(i=2;i<=n;i++)
{
f3=f1+f2;
f1=f2;
f2=f3;
document.write(f3+"<br/>");
}
</script>
<style>
body {background-color:#66ff66}
</style>
</head>
</body>