javascript - 尝试使用 javascript 查找素数之和时出现错误

标签 javascript arrays for-loop primes

我正在尝试获取素数数组的总和,并且我知道有更优雅的方法可以做到这一点,并且已经看到了这些解决方案的链接。

我的问题是这个特定脚本中出现了错误,我正在尝试了解导致此代码失败的原因。

问题是数字 9、15 和许多其他数字都被添加到素数数组中,尽管它们都正确地未能通过检查它们是否是素数的测试。尽管测试失败,但我无法理解脚本中导致数字推送到数组的原因。再说一次,我并不是在寻找一种完全不同/更好的方法来对素数求和,但如果能帮助我们确定这个脚本中到底出了什么问题,我将不胜感激。

function totalPrime(num) {
  var nums = [];
  var primes = [];

  for (var i = 1;
    (num - i) > 1; i++) {
    nums.push(num - i);
  }

  nums.forEach(isPrime);

  function isPrime(n) {
    var a = [];
    var test;
    if (n === 1) {} else if (n === 2) {
      primes.push(n);
    } else {
      for (var i = 1;
        (n - i) > 1; i++) {
        a.push(n - i);
      }
      a.forEach(function(x) {
        if ((n % x) === 0) {
          test = false;
        } else {
          test = true;
        }
      });
      if (test) {
        primes.push(n);
      } else {}
    };
  }

  console.log(primes.reduce(function(a, b) {
    return a + b
  }));
}

totalPrime(5);

与我用来调试的日志记录相同的脚本:

function totalPrime(num) {
  var nums = [];
  var primes = [];

  for (var i = 1;
    (num - i) > 1; i++) {
    nums.push(num - i);
  }

  nums.forEach(isPrime);


  function isPrime(n) {
    var a = [];
    var test;
    if (n === 1) {
      console.log(n + ' is NOT a prime number');
    } else if (n === 2) {
      console.log(n + ' IS a prime number');
      primes.push(n);
    } else {
      for (var i = 1;
        (n - i) > 1; i++) {
        a.push(n - i);
      }
      a.forEach(function(x) {
        if ((n % x) === 0) {
          test = false;
          console.log(n + ' % ' + x + ' equals 0');
          console.log(x + ' fails check');
        } else {
          test = true;
          console.log(n + ' % ' + x + ' does NOT equal 0');
          console.log(x + ' passes check');
        }
      });
      if (test) {
        console.log(n + ' IS a prime number.');
        primes.push(n);
      } else {
        console.log(n + ' is NOT a prime number.');
      }
    };
  }

  console.log(primes);
  console.log(primes.reduce(function(a, b) {
    return a + b
  }));
}

totalPrime(5);

最佳答案

每个测试中的 test 值都会覆盖之前的检查。因此,实际上只有最后一个检查(除以 2)变得相关,并且所有奇数素数都失败。

您可以通过将test的默认值更改为true来更正它,并删除代码test = true;中存在的行。

更正后的代码:

function isPrime(n) {
  var a = [];
  var test = true;
  if (n === 1) {} else if (n === 2) {
    primes.push(n);
  } else {
    for (var i = 1;
      (n - i) > 1; i++) {
      a.push(n - i);
    }
    a.forEach(function(x) {
      if ((n % x) === 0) {
        test = false;
      } 
    });
    if (test) {
      primes.push(n);
    }
  };
}

关于javascript - 尝试使用 javascript 查找素数之和时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49781213/

相关文章:

javascript - 获取所选数据表行的 ID 并将其传递给多行删除函数

javascript - getQuote() 函数完成后更改 #new-quote 元素的外层高度

php - 日期时间加 1 天

R for循环索引问题

java - 如何重构银行方法中的循环

javascript - 从 js 控制 iframe 边距和源(iframe 中的 js)

javascript - CodeMirror 'mode' 可以用颜色替换标签吗?

java - 代码运行一段时间后出现 IndexOutOfBoundsException

javascript - 仅在必要时将数字四舍五入至小数点后两位

python - 交错形状不匹配的 NumPy 数组