gpt4 book ai didi

javascript - 打印从 a 到 b 的素数,a 到 b 是用户使用 javascript 输入的值

转载 作者:行者123 更新时间:2023-11-30 20:13:41 25 4
gpt4 key购买 nike

我试过了,但只能得到 1 到 50 或 1 到 100 个素数。我应该如何正确找出质数。用户问了哪些?

    <h1>enter the no.</h1><input type="text" id="limit">
<h1>enter the no.</h1><input type="text" id="limit2">
<button onclick="fun()">Submit</button>
<div id="result"></div>
<script type="text/javascript">
function fun() {
var i = limit;
var j = limit2;
limit = document.getElementById('limit').value;
limit2 = document.getElementById('limit2').value;
for (i = limit; i <= limit2; i++) {
c = 0;

for (j = 1; j <= i; j++) {
if (i % j == 0) {
c++;
}
}

if (c == 2) {
document.getElementById("result").insertAdjacentHTML('beforeend', i + '<br>');
}
}
}
</script>

最佳答案

找出下面的更正

function fun() {
/*var i = limit;*/
/*var j = limit2;*/
var limit = document.getElementById('limit').value;
var limit2 = document.getElementById('limit2').value;
var result = document.getElementById("result");
result.innerHTML = "Result: ";
for (var i = limit; i <= limit2; i++) {
var prime = true;
/* set j = 2 and NOT j = 1 */
for (var j = 2; j < i; j++) {
if (i % j == 0) {
prime = false;
break;
}
}

if (prime) {
result.insertAdjacentHTML('beforeend', i + ','); /* replaced <br/> with , to avoid page scroll */
}
}
}
fun(); /* test pupose only */
h1 {
margin: 0px;
}
<h1>enter the no.</h1><input type="text" id="limit" value="12">
<h1>enter the no.</h1><input type="text" id="limit2" value="55">
<button onclick="fun()">Submit</button>
<div id="result">Result:</div>

关于javascript - 打印从 a 到 b 的素数,a 到 b 是用户使用 javascript 输入的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52138343/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com