简体   繁体   English

用JS查找素数序列

[英]Finding sequence of prime numbers with JS

I have to get a sequence of prime numbers.我必须得到一个质数序列。 But my code does not work.但是我的代码不起作用。 How is it possible to fix it?怎么可能修复它?

var num1 = parseInt(prompt('Enter a number'));
var num2 = parseInt(prompt('Enter a number'));
var num3 = 0;
function primeSeq(num1, num2) {  
var b = 1;
var c = '';
if (num1 > num2) {
num3 = num2;
num2 = num1;
num1 = num3;
        }
for (var i = num1; i < num2; i++) {
for (var j = 2; j < i; j++) {
if (i % j == 0) {
b++;
}
if (b <= 1) {
c += i + ' ';               
}
}
}
return c;
}
alert(primeSeq(num1, num2));

I guess you wanted something like this我猜你想要这样的东西

var num1 = parseInt(prompt('Enter a number'));
var num2 = parseInt(prompt('Enter a number'));
var num3 = 0;
if (num1 > num2) {
  num3 = num2;
  num2 = num1;
  num1 = num3;
}
function primeSeq(num1, num2) {  
  var b;
  var c = '';
  for (var i = num1; i < num2; i++) {
    b = 1;
    for (var j = 2; j < i; j++) {
      if (i % j === 0) {
        b++;
      }
    }     
    if (b === 1) {
      c += i + ' ';               
    }
  }
  return c;
}
alert(primeSeq(num1, num2));

So in short, b should reset to 1 on every new prime candidate ( i loop) and check of b should be outside of inner ( j ) loop.所以简而言之, b应该在每个新的主要候选者( i循环)上重置为 1 并且对b检查应该在内部( j )循环之外。

Please note that there are more optimal algorithms.请注意,还有更多最佳算法。

The lot easier way is to use a sieve system, if a number is divisible by another prime number it is not a prime.更简单的方法是使用筛分系统,如果一个数可以被另一个素数整除,它就不是素数。 You can write a function like this:你可以写一个这样的函数:

function primes(max) {
    let primes = [2];
    for (let i = 3; i <= max; i++) {
        let found = true;
        for (let j = 0; j < primes.length; j++) {
            if (i % primes[j] == 0) found = false;
        }
        if (found) primes.push(i);
    }
    return primes;
}

Explanation说明

What you know by default is that 2 is a prime, so you start at 3. You don't want to exeed the max, that is the i <= max statement.默认情况下,您知道 2 是素数,因此您从 3 开始。您不想超出最大值,即i <= max语句。 Assume it is a prime, then search in the array if it is divisible by primes you found before, if that is the case, set found to false.假设它是一个素数,然后在数组中搜索它是否可以被您之前找到的素数整除,如果是这样,则将 found 设置为 false。 Now check if is was found, push is to the array and return the primes.现在检查是否找到了,推送到数组并返回素数。

Here is bit more optimized algorithm:这是更优化的算法:

function primeSeq(num1, num2) {
  var primes = [];
  var isPrime;
  var j;
  var results = [];

  for (var i = 2; i < num2; i++) {
    isPrime = true;
    j = 0;
    while (j < primes.length) {
      if (i % primes[j] === 0) {
        isPrime = false;
        break;
      }
      j++;
    }
    if (isPrime) {
      primes.push(i);
      if (i >= num1) {
        results.push(i);
      }
    }
  }
  return results.join(' ');
}

In order for number to be prime it must not be dividable with all the smaller primes, so we are generating an array of primes to check upon.为了使数字成为素数,它不能与所有较小的素数相除,因此我们生成了一个primes数组进行检查。 There is a theory also that every prime bigger than 3 has a following form:还有一种理论认为每个大于 3 的质数都有以下形式:

6k+1 or 6k-1

So this would simplify it bit more.所以这会更简化它。

Try this one试试这个

<input ng-model="range" type="number" placeholder="Enter the range">
 <button ng-click="findPrimeNumber(range)">


    $scope.findPrimeNumber=function(range){
         var tempArray=[];
         for(var i=0;i<=range;i++){
            tempArray.push(i)
         }
         var primenumbers= tempArray.filter((number) => {
         for (var i = 2; i <= Math.sqrt($scope.range); i++) {
            if (number % i === 0) return false;
          }
          return true;
         });
         console.log(primenumbers);

     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM