Prime检查程序不对某些数字起作用

时间:2018-04-27 20:58:56

标签: javascript html

我很兴奋,因为我的黄金号码计划正在运作,但当我尝试某些数字时,如 5 10 2 并且 11 ,它无法正常工作。我试图弄清楚自己,并通过查看与我正在做的事情相关的其他事情,但我无法弄清楚我很感激答案感谢所有帮助的人,因为我只是编码的初学者,想要了解更多信息。

var minimum;
var maximum;
var primes;

function myFunction(e) {
  e.preventDefault();
  minimum = document.getElementById('Minimum').value;
  maximum = document.getElementById('Maximum').value;
  primes = [];

  listOfPrimes(minimum, maximum); 
  primes = primes.filter(function(x) {
    return x > 1;
  });
  primes = primes.toString();
  primes = primes.replace(/,(?=[^\s])/g, ", ");
  document.getElementById("Your_Primes").innerHTML = primes;
}

function isPrime(num) {
  for (var i = 2; i <= num / 2; i++)
    if (num % i === 0) {
      return false;
    }
  primes.push(num);
}

function listOfPrimes(min, max) {
  for (var j = min; j <= max; j++) {
    isPrime(j);
  }
}
body {
	background-color: #dbdbdb;
}

#prime-checker {
	background-color: #89a9dd;
	margin: 5px;
	padding: 5px;
	text-align: center;
}

.the-primes {
	background-color: #587cb7;
	padding: 15px;
	margin: 10px;
}
<!doctype html>

<html>
<head>
	<title>Prime Lister</title>
	<link href="main.css" rel="stylesheet" type="text/css">
</head>
<body>

<div id="prime-checker">

<form>
  <h2>Minimum Number:</h2><br>
  <input type="number" placeholder="MinimumWholeNumber" id="Minimum" multiple="1" min="0" required><br><br> 
  <h2>Maximum Number:</h2><br>
  <input type="number" placeholder="MaximumWholeNumber" id="Maximum" multiple="1" min="0" required><br><br><br>
  <button id="button" onclick="myFunction(event)">Enter</button>
</form>


<div class="the-primes"><h2>Your Primes:</h2>
  <p id="Your_Primes"></p>
</div>

</div>

<script type="text/javascript" src="Web_Prime_Lister.js"></script>
</body>
</html>

2 个答案:

答案 0 :(得分:5)

您计算素数的代码看起来很好。问题是您使用最小值和最大值的原始字符串来创建范围。问题是字符串是以字面方式进行比较的,所以:

"2" < "100" // false

因此,当您将这些值传递给函数listOfPrimes时,由于字符串比较,该循环将提前终止。

要修复它,只需在输入值之前将其转换为数字:

minimum = Number(document.getElementById('Minimum').value);
maximum = Number(document.getElementById('Maximum').value);

这里有你修补的代码片段,一切看起来都运行良好:

&#13;
&#13;
var minimum;
var maximum;
var primes;

function myFunction(e) {
  e.preventDefault();
  minimum = Number(document.getElementById('Minimum').value);
  maximum = Number(document.getElementById('Maximum').value);
  primes = [];

  listOfPrimes(minimum, maximum); 
  primes = primes.filter(function(x) {
    return x > 1;
  });
  primes = primes.toString();
  primes = primes.replace(/,(?=[^\s])/g, ", ");
  document.getElementById("Your_Primes").innerHTML = primes;
}

function isPrime(num) {
  for (var i = 2; i <= num / 2; i++)
    if (num % i === 0) {
      return false;
    }
  primes.push(num);
}

function listOfPrimes(min, max) {
  for (var j = min; j <= max; j++) {
    isPrime(j);
  }
}
&#13;
body {
	background-color: #dbdbdb;
}

#prime-checker {
	background-color: #89a9dd;
	margin: 5px;
	padding: 5px;
	text-align: center;
}

.the-primes {
	background-color: #587cb7;
	padding: 15px;
	margin: 10px;
}
&#13;
<!doctype html>

<html>
<head>
	<title>Prime Lister</title>
	<link href="main.css" rel="stylesheet" type="text/css">
</head>
<body>

<div id="prime-checker">

<form>
  <h2>Minimum Number:</h2><br>
  <input type="number" placeholder="MinimumWholeNumber" id="Minimum" multiple="1" min="0" required><br><br> 
  <h2>Maximum Number:</h2><br>
  <input type="number" placeholder="MaximumWholeNumber" id="Maximum" multiple="1" min="0" required><br><br><br>
  <button id="button" onclick="myFunction(event)">Enter</button>
</form>


<div class="the-primes"><h2>Your Primes:</h2>
  <p id="Your_Primes"></p>
</div>

</div>

<script type="text/javascript" src="Web_Prime_Lister.js"></script>
</body>
</html>
&#13;
&#13;
&#13;

PS :您只需检查isPrime之前的除数即可进一步优化Math.floor(Math.sqrt(num))函数。这将为您节省许多次迭代,特别是对于大量迭代。所以:

function isPrime(num) {
  var max = Math.floor(Math.sqrt(num));
  for (var i = 2; i <= max; i++)
    if (num % i === 0) {
      return false;
    }
  primes.push(num);
}

正如您所看到的,对于10位数的素数,运行时间大大减少:

&#13;
&#13;
function isPrimeSqrt(num) {
  var max = Math.floor(Math.sqrt(num));
  for (var i = 2; i <= max; i++)
    if (num % i === 0) {
      return false;
    }
  return true;
}

function isPrimeHalf(num) {
  var max = Math.floor(num / 2);
  for (var i = 2; i <= max; i++)
    if (num % i === 0) {
      return false;
    }
  return true;
}

let now = Date.now();
const pHalf = isPrimeHalf(1010189899);
console.log("Using 'Half' took:", Date.now() - now, "ms. Is prime:", pHalf);
now = Date.now()
const pSqrt = isPrimeSqrt(1010189899);
console.log("Using 'Sqrt' took:", Date.now() - now, "ms. Is prime:", pSqrt);
&#13;
&#13;
&#13;

答案 1 :(得分:1)

知道了! 只需更改此行:

  for (var j = min; j <= max; j++) {

到此:

  for (var j = parseInt(min); j <= parseInt(max); j++) {

熟悉调试工具(在大多数浏览器中为F12)。在那里你可以暂停某些行的执行并检查变量的值和类型。如果你这样做,那么你会看到min = "5"min = 5。这就是为什么5-49不起作用,因为“5”小于“49”(因为它的第一个数字较低),所以它根本不循环。