Rich
Rich

Reputation: 3336

Bug while trying to find sum of primes with javascript

I am trying to get the sum of an array of prime numbers, and I understand there are more elegant ways to do that and have seen the links to those solutions.

My problem is something that's wrong within this specific script, and I'm trying to understand what's causing THIS code to fail.

The issue is that the numbers 9, 15 and many others are being being added into the primes array, even though they all, correctly, fail a test to check if they're prime numbers. I can't wrap my head around what in the script is causing the numbers to push to the array despite failing that test. Again, I'm not looking for a completely different/better approach to summing the primes, but some help in identifying what exactly is wrong in this script would be really appreciated.

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);

Same script with logging I was using to debug:

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);

Upvotes: 2

Views: 76

Answers (1)

user9215105
user9215105

Reputation:

Your test value in each test override the previous check. Thus, actualy only the last check (divide in 2) become relevant, and all the odd primes fail.

You can correct it by change the default of test to true, and remove the exist line in the code test = true;.

The corrected code:

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);
    }
  };
}

Upvotes: 2

Related Questions