【问题标题】:Bug while trying to find sum of primes with javascript尝试使用 javascript 查找素数总和时出现错误
【发布时间】:2018-09-21 16:49:54
【问题描述】:

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

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

问题是数字 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);

【问题讨论】:

  • a.forEach(function(x) { 中设置test 变量可确保其在调用后的最终值将是在其last 迭代中设置的值。
  • 我想你的意思是在.forEach之前初始化test = true,然后在回调中仅将其设置为false
  • 添加到 @CrazyTrain 所说的内容,您将其设置为 false 在 9 % 3 但在 9 % 2 处转身并将 test 设置为 true。在我看来,最好使用 for 循环并在发现错误后立即中断循环。这不仅可以完成工作,而且当您处理更大的数字时,它会阻止您在第 2 次迭代时意识到它是错误的时必须经历所有迭代。
  • 如果你对变量使用像nxa这样的神奇名称,则很难阅读代码。另请阅读algorithm 以了解检查数字是否为素数的有效方法。我建议在任何浏览器中使用 DEBUG 来查看代码有什么问题。

标签: javascript arrays for-loop primes


【解决方案1】:

您在每个测试中的 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);
    }
  };
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-09
    • 2017-07-11
    • 2021-06-04
    • 1970-01-01
    • 2016-10-08
    • 2020-01-14
    • 1970-01-01
    相关资源
    最近更新 更多