I am not sure how to complete every single check for prime numbers. There are other number they can be divided by within my function (I assume).
Is it possible to get some kind of hint on how to go on ?
Your code so far
function sumPrimes(num){
let result = 2;
if(num <= 1){
return 0
}
else if(num == 2){
return 2
}
for(let i = 3; i <= num; i++){
if(i % 2 == 0){
//Do nothing, /2 isnt prime
}
else{
if(i % 3 === 0 && i != 3){
console.log(i+ " is NO prime")
}
else if(i % 5 === 0 && i != 5){
console.log(i+ " is NO prime")
}
else if(i % 7 === 0 && i != 7){
console.log(i+ " is NO prime")
}
else if (i == 3 || i == 5 || i == 7){
console.log(i + " is Prime so ADD")
result += i;
}
else{
console.log(i + " is Prime so ADD")
result += i;
}
}
}
return result;
}
console.log("result "+ sumPrimes(977))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:148.0) Gecko/20100101 Firefox/148.0
Challenge Information:
Build a Prime Number Sum Calculator - Build a Prime Number Sum Calculator
I’m not sure if there’s any single universal check that can catch all kinds of primes. Notice that for any prime, ie. 127 function will earlier find all lower primes. Could that be used somehow in later checks?
To be exact, prime cannot be divided by prime, and all factors of a non-prime number can be reduced to a prime number. So checking if number can be divided by primes lower than it would be enough to confirm whether it’s prime or not.
I can’t understand what is happening now. I created an array to put found prime in. But it still doesn’t work. What did i do wrong ?
function sumPrimes(num){
let foundPrimes = [];
let result = 2;
if(num <= 1){
return 0
}
else if(num == 2){
return 2
}
for(let i = 3; i <= num; i+=2){
if(i % 3 === 0 && i != 3){
console.log(i+ " is NO prime")
}
else if(i % 5 === 0 && i != 5){
console.log(i+ " is NO prime")
}
else if(i % 7 === 0 && i != 7){
console.log(i+ " is NO prime")
}
else if (i == 3 || i == 5 || i == 7){
console.log(i + " is Prime so ADD")
result += i;
}
else{
for(let prime of foundPrimes){
if(i % prime == 0){
console.log(i+" divided by "+prime+" so OUT")
i+=2;
}
}
console.log(i + " is Prime so ADD")
result += i;
foundPrimes.push(i);
}
}
return result;
}
console.log("result "+ sumPrimes(977));
else{
for(let prime of foundPrimes){
if(i % prime == 0){
console.log(i+" divided by "+prime+" so OUT")
i+=2;
}
}
console.log(i + " is Prime so ADD")
result += i;
foundPrimes.push(i);
}
Could you explain how the added loop is supposed to work? What happens when number is deemed to not be a prime?
Yeah, i’ve tried putting an else statement but it breaks the whole program !
non-prime are supposed to be skipped.
Thing is, 125 divided by 5 is a whole number but why dosn’t it detect and shoot it out ?
(else that breaks everything like so
for(let prime of foundPrimes){
if(i % prime == 0){
console.log(i+" divided by "+prime+" so OUT")
i+=2;
}
else{
console.log(i + " is Prime so ADD")
result += i;
foundPrimes.push(i);
}
}
Notice that loop doesn’t stop when it encounters first number that’s dividing i. The else adds number to foundPrimes when one prime is not dividing the i, but there might be other primes that are dividing it.
for(let prime of foundPrimes){
if(i % prime == 0){
console.log(i+" divided by "+prime+" so OUT")
i+=2;
continue;
}
}
console.log(i + " is Prime so ADD")
result += i;
foundPrimes.push(i);
}
It is supposed to start from 3 and loop up until the last found prime to check every prime already found. Is it ?
I think i comprehend it now. The continue will continue inside the for loop of prime instead of the for loop of every number, is that right ?
What i want from this is the for (let prime of foundPrimes) to stop and go on to the next itteration of the outer loop. And it dose not happen ?
so what i did now is so :
outer : for(let i = 3; i <= num; i+=2){
if(i % 3 === 0 && i != 3 || i % 5 === 0 && i != 5 || i % 7 === 0 && i != 7){
console.log(i+" is no Prime")
}
else if(i == 3 || i == 5 || i == 7){
console.log(i + " is Prime so ADD")
result += i;
}
else{
inner : for(let prime of foundPrimes){
if(i % prime == 0){
console.log(i+" divided by "+prime+" so OUT")
i+=2;
continue outer;
}
}
console.log(i + " is Prime so ADD")
result += i;
foundPrimes.push(i);
}