## Sum of all primes numbers

## Sum of all primes numbers

(OP)

hello,

Can someone help me please with this algorithm?

my example is 10.

(A prime number is a whole number greater than 1 with exactly two divisors: 1 and itself. For example, 2 is a prime number because it is only divisible by 1 and 2. In contrast, 4 is not prime since it is divisible by 1, 2 and 4.

Rewrite sumPrimes so it returns the sum of all prime numbers that are less than or equal to num.)

function sumPrimes(num) {

var x=2;

var added=2;

while (x<num) {

x++;

for (var i=2;i<x;i++){

if (x%i===0){

break;

}

else if (i===x-1) {

added += x;

}

}

}

return added;

}

but I am lost whem x =4.

x++; (x=3)

i=2;

(i===x-1); (2 === 3 -1 )

added += 3;

...

then

x++; x=4;

i=3;

(i===x-1); (3 === 4 -1 )

added += 4; But... 4 should not be add since is about all primes numbers <10.

Can someone help me please?

Thank you in advance!

Can someone help me please with this algorithm?

my example is 10.

(A prime number is a whole number greater than 1 with exactly two divisors: 1 and itself. For example, 2 is a prime number because it is only divisible by 1 and 2. In contrast, 4 is not prime since it is divisible by 1, 2 and 4.

Rewrite sumPrimes so it returns the sum of all prime numbers that are less than or equal to num.)

function sumPrimes(num) {

var x=2;

var added=2;

while (x<num) {

x++;

for (var i=2;i<x;i++){

if (x%i===0){

break;

}

else if (i===x-1) {

added += x;

}

}

}

return added;

}

but I am lost whem x =4.

x++; (x=3)

i=2;

(i===x-1); (2 === 3 -1 )

added += 3;

...

then

x++; x=4;

i=3;

(i===x-1); (3 === 4 -1 )

added += 4; But... 4 should not be add since is about all primes numbers <10.

Can someone help me please?

Thank you in advance!

## RE: Sum of all primes numbers

I do not get you. For sumPrimes(10) your code returns 17, which according to my calculation is correct. And no, 4 is not included in that 17, as your code correctly not adds it.

Feherke.

feherke.github.io

## RE: Sum of all primes numbers

but I don't understand the part where:

x++; x=4;

i=3;

I don't know how my the code does not add 4 beause the condition of ``else if `` is applied:

(i===x-1); (3 === 4 -1 )

added(which is 5 ) += 4 (is reciving 4 becouse is executing this line of code else if (i===x-1) ; and added it will be now 9).

this is how i use it:

x++; x=4;

i=3;

(i===x-1); (3 === 4 -1 )

added(5)+= 4;

x++; x=5;

i=4;

(i===x-1); (4 === 5 -1 )

added(9)+= 5; and so on...

## RE: Sum of all primes numbers

Huh ? When testing x = 4 the else if (i===x-1) line is not executed. It would be executed when i = 3, but it breaks out from the for loop when i = 2.

Here I added some console.log()s to your code :

## CODE --> CrxC01.js

Then when running it produces this output :

## CODE --> console

`master # nodejs CrxC01.js ADDED 3 skipped 4 because divisible with 2 ADDED 5 skipped 6 because divisible with 2 ADDED 7 skipped 8 because divisible with 2 skipped 9 because divisible with 3 skipped 10 because divisible with 2 Result 17`

Feherke.

feherke.github.io

## RE: Sum of all primes numbers

I thought that i is increased with every iteration and that’s why I thought i = 3, because when x = 4, was the second iteration and that it would be i = 3.

but now I still don't understand, if x = 5, the i is still 2? beacouse if i is still 2, the condition of (i === x - 1) is not met, (2 === 5-1) and added will not recive 5.

I am sorry if I miss something, you explanation is very good.

## RE: Sum of all primes numbers

Unsure what exactly are you not understanding there.

There are 2 loops : the outer while loop and the inner for loop :

for each x valueover the values from 2 to x - 1Feherke.

feherke.github.io

## RE: Sum of all primes numbers