Sum All Primes - How to Solve Without Using Array

Hello All,

I’ve solved the Sum All Primes challenge after a lot of testing and tweaking of the For loops. It passes the tests and works, but I think it’s pretty clunky with the way it adds so many “true” and “false” entries to the array for every number, especially the higher numbers.

Is there a way I can change my existing code to get rid of the array? I couldn’t figure out how to get the For loops to make count+= i ONLY when NONE of the numbers greater than 1 and less than the number being tested for primality pass if (i % j === 0) {}

Please see my code below with comments. Thank you in advance for any insights you can give a very new programmer.

function sumPrimes(num) {
let count = 0; 
let isComposite;
let trueFalseArr = [];

for (let i = 3; i <= num; i++) {
trueFalseArr.splice(0)      

 //resets the array for every iteration of i

for (let j = 2; j < i; j++) {
if (i % j === 0) { 
  isComposite = "true";
  trueFalseArr.push(isComposite);
}  

//Since prime numbers are only divisible by themselves and 1, if i % j === 0 for any number greater than 1 and less than i, we know it is a composite number, so the isComposite variable = "true" and gets pushed to trueFalseArr. 


else {
  isComposite = "false";
  trueFalseArr.push(isComposite);
}


if (j === i - 1) {
  if (trueFalseArr.includes("true") === false) {
    count += i; 
  }

//At this point all numbers greater than 1 and less than i have been tested to see if any divide evenly. Any that do make "true" get added to the array. This checks to see if "true" is included in the array andwhere, and if it is NOT included, we know it's a prime, so it adds i to count.

}
}
}
return count + 2
}

console.log(sumPrimes(977));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Sum All Primes

Link to the challenge:

Formatted:

function sumPrimes(num) {
  let count = 0;
  let isComposite;
  let trueFalseArr = [];

  for (let i = 3; i <= num; i++) {
    trueFalseArr.splice(0)
    //resets the array for every iteration of i
    for (let j = 2; j < i; j++) {
      if (i % j === 0) {
        isComposite = "true";
        trueFalseArr.push(isComposite);
      }
      //Since prime numbers are only divisible by themselves and 1, if i % j === 0 for any number greater than 1 and less than i, we know it is a composite number, so the isComposite variable = "true" and gets pushed to trueFalseArr. 
      else {
        isComposite = "false";
        trueFalseArr.push(isComposite);
      }
      if (j === i - 1) {
        if (trueFalseArr.includes("true") === false) {
          count += i;
        }
        //At this point all numbers greater than 1 and less than i have been tested to see if any divide evenly. Any that do make "true" get added to the array. This checks to see if "true" is included in the array andwhere, and if it is NOT included, we know it's a prime, so it adds i to count.
      }
    }
  }
  return count + 2
}

console.log(sumPrimes(977));

Ok, there exists an efficient algorithm uses an array, but this is a less efficient implementation of the less efficient algorithm.

You don’t need to check this many numbers less than i

Why do you have an array here? You already know that the number is composite, so you can just stop here

I need to make my code go to the next iteration of i at this point, correct? How do I do that? Sorry I’m sure it’s a simple question, but I’m not sure.

You could could also do this by keeping a single boolean flag that indicates if the current number is composite or not.

Thanks I did try that originally, but then couldn’t get the else statement to work correctly.

Basically, I couldn’t figure out how to check specifically at the END of the i iteration loop to see if isComposite = true or isComposite = false, and then add to i to count.

Obviously when it goes through the whole iteration of i and j it’s going to get a lot of falses for composite numbers along with trues. How can I make i only get added to count if ALL iterations of i return false for the test i % j === 0?

That’s what I couldn’t figure out.

for (i = 2; i < 100; i++) {
  // This is before the inner loop
  for (j = 2; j < i; j++) {
    // This is inside of the inner loop
  }
  // This is after the inner loop
}

You need to put command before, inside, or after the inner loop depending upon that effect you want.

You’re the man, thank you! I was able to get it working now without the array. I feel silly, I just had my final IF statement inside the inner loop when it should have been after the inner loop but inside the outer loop. The code below works now.

  let count = 0; 
  let isComposite;

for (let i = 3; i <= num; i++) {
isComposite = "false";
for (let j = 2; j < i; j++) {
  if (i % j === 0) {
    isComposite = "true";
    break;
  }
}
if (isComposite === "false") {
  count += i;
}
}
return count + 2
}
console.log(sumPrimes(977));```
1 Like

Huzzah :tada:

These can be a bit tricky of problems; nice work trimming the solution down!

1 Like

If you make isComposite a Boolean instead of a String, you could write your if statement as:

if (!isComposite) {
  count += i;
}

Also, instead of count, I would use sum because you are creating a sum of prime numbers and not a count.

One last thing. Since you only use isComposite inside the outer for loop, why not just declare it false there instead of outside the outer for loop? You end up with one less line.

1 Like