Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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!


What I have tried:

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 ) 
Posted
Updated 25-Mar-21 11:31am
Comments
Richard MacCutchan 25-Mar-21 8:34am    
Your loop should go from 3 to N, increment 2, so that it will avoid all even numbers. For each number in the loop you need to divide it by X, where X is in range [ 3 .. N/2 ] again incrementing by 2 to avoid even numbers.
crx10 25-Mar-21 9:19am    
Thank you so much!

Your question title says you want the sum of all prime numbers. Since there are an infinite number of them, the only correct solution is:
JavaScript
function sumOfAllPrimes(){
    return Infinity;
}
Infinity - JavaScript | MDN[^]

If you simply want to sum the primes less than a given limit, then use something simple like the Sieve of Eratosthenes[^]. Google will find you plenty examples of how to write one in code.
 
Share this answer
 
Comments
Maciej Los 25-Mar-21 9:12am    
5ed!
crx10 25-Mar-21 9:19am    
Thank you so much!
Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
JavaScript
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;
}

Indentation style - Wikipedia[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
Enabling Open Innovation & Collaboration | The Eclipse Foundation[^]
-----
As a beginner, you make a common mistake, you make monolithic code, everything is in a single function, the problem, it complicate writing and testing.
Look at this
JavaScript
function sumPrimes(num) {
	var added=0;
	for (var i=2; i<num; i++){
		if (IsPrime(i)){
			added += i;
		}
	}
	return added;
}

function IsPrime(num) {
	for (var i=2; i<num; i++){
		if (num%i===0){
			return false;
		}
	}
	return true;
}
 
Share this answer
 
Comments
crx10 26-Mar-21 1:29am    
Thank you so much!
from now on I will definitely structure my code in a properly way, and I hope that I will be able to write code in a more advance way like you showed me.
Thank you so much for you help!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900