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!