*Intermediate Algorithm Scripting: Sum All Primes

How may I change my let oddNumbersArr=newArr.filter(e=>e%2==1) to get only prime numbers?

function sumPrimes(num) {
 
 let newArr=[];
 
 for(let i=0;i<=num;i++){

        newArr.push(i)       

 }
    
 let oddNumbersArr=newArr.filter(e=>e%2==1)

 (elements=>elements>1) 

let sumOfAll=greaterThanOneArr.reduce((a,b)=>a+b,0)
  
  console.log(sumOfAll)  
}

sumPrimes(10);

You could make a boolean array of all primes via the Sieve of Eratosthenes. That then becomes a boolean test value for you.

1 Like

How can I do this with every number from 2 until num???

function sumPrimes(num) {

 let integersNumbersArr= []    
    
    for(let i=2;i<=num;i++){
             
             if(Number.isInteger(num/i)){

               integersNumbersArr.push(i)       

             }
    
    }
 
 console.log(integersNumbersArr)
 let numbersArr = [];
      
      if(integersNumbersArr.length==1){
                    
                    numbersArr.push(integersNumbersArr)
      }

Can you explain, in words, how you want this code to work? I don’t think that checking for divisors of num is what you want.

Yes., I can explain it. I can get the prime number ef a number with it. My problem is the next one: I cannot do it with every number from 2 until num. For example: If I set 10, I cannot get nothing because it is not prime, but if I set 2, I can get 2 because it can be divided by itself, therefore it is a prime number and It cannot be diveded by another number.

Because your function only tests num. It doesn’t test any other values.

1 Like

Exactly. I want to change that.

Your function makes a list of numbers that divide evenly into num. That will not be a list of prime numbers. Prime numbers have no divisors except for 1 and themselves.

No. That is not the end of the function. You get a prime number. That is the reason why i want to do it from 2 until num and I do not know how to.

From what I’m seeing, you don’t get a prime number. You get all numbers that divide evenly into num

Ah, never mind. I see it now!

Ah, I see. Why make a list? Why not stop as soon as any number divides your number?

This approach is slow and expensive, but you can wrap that logic in an outer loop and replace num in the i loop bound with the outer loop variable. It’ll work but will be very slow for large num.

I still think the seive algorithm is easier to code.

Note: I was serious when I asked you to explain your logic. Converting code to a verbal explanation is an important debugging technique. I really was hoping that you would write out the logic in words.

1 Like

I know it, but I did not know the process to do so. I have done what I could understand :frowning:

How can I do that with tat outer loop? I tried it, but I only get the same result multiplied by num

Do you know how to nest loops?

for (let i = 0; i <= num; i++) {
  for (let j = 0; j < i; j++) {
 }
}
1 Like

I do not know remeber it if I have done it before. Is that way?

Yup, that’s an example of a nested loop. If you use your inner loop to test if a number is prime and use the outer loop to cover all numbers from 1 to num, it will work (albeit slowly).

1 Like

I am using it. Please, wait.

1 Like

I am having the same problem :frowning:

function sumPrimes(num) {

for (let i = 0; i <= num; i++) {

let integersNumbersArr= []    
    
    for(let j = 0; j <= i; j++){
             
             if(Number.isInteger(num/j)){

               integersNumbersArr.push(j)       
         }
     }
 console.log(integersNumbersArr)
      let numbersArr = [];
       if(integersNumbersArr.length==1){
                numbersArr.push(integersNumbersArr)
      }
 console.log(numbersArr)  
 }
   
}

sumPrimes(19)

You are checking num each inner loop. You will get (partial) results for the primality of num unless you use the outer loop to change which number you are checking.

I am getting the numbers, but I do not under the reason why it is not pushing them in the array.

function sumPrimes(num) {

for (let i = 2; i <= num; i++) {
//console.log(i)
let integersNumbersArr= []    
    
    for(let j = 2; j <= i; j++){
  //console.log(j)           
             if(Number.isInteger(i/j)){

               integersNumbersArr.push(i)       
         }
     }
 //console.log(integersNumbersArr)
      let numbersArr = [];
       if(integersNumbersArr.length==1){
                numbersArr.push(integersNumbersArr)
      }
 console.log(numbersArr)  
 }
         
   }
sumPrimes(19)