×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Sum of all primes numbers

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!

RE: Sum of all primes numbers

Hi

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

(OP)
Thank you for your answear.

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

Hi

Quote (CrxC01)

(is reciving 4 becouse is executing this line of code else if (i===x-1) ; and added it will be now 9).
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

function sumPrimes(num)
{
	var x = 2;
	var added = 2;
	while (x < num) {
		x++;
		for (var i = 2; i < x; i++) {
			if (x % i === 0) {
console.log('skipped', x, 'because divisible with', i)
				break;
			} else if (i === x - 1) {
console.log('ADDED  ', x)
				added += x;
			}
		}
	}
	return added;
}

console.log('Result ', sumPrimes(10)); 

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

(OP)
THANK YOU SO MUCH!!!!

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

Hi

Unsure what exactly are you not understanding there.

There are 2 loops : the outer while loop and the inner for loop :
  • The while loop iterates once over the values from 2 to num - 1
  • The for loop iterates once for each x value over the values from 2 to x - 1
╔════════════╦════════════════════════════════════════╗
 while loop          for loop                       
 value of x  value of i  condition met             
╠════════════╬════════════╬═══════════════════════════╣
          2           2  x divisible by i -> break 
├────────────┼────────────┼───────────────────────────┤
          3           2  last i tested -> is prime 
├────────────┼────────────┼───────────────────────────┤
          4           2  x divisible by i -> break 
├────────────┼────────────┼───────────────────────────┤
          5           2                            
                      3                            
                      4  last i tested -> is prime 
├────────────┼────────────┼───────────────────────────┤
          6           2  x divisible by i -> break 
├────────────┼────────────┼───────────────────────────┤
          7           2                            
                      3                            
                      4                            
                      5                            
                      6  last i tested -> is prime 
├────────────┼────────────┼───────────────────────────┤
          8           2  x divisible by i -> break 
├────────────┼────────────┼───────────────────────────┤
          9           2                            
                      3  x divisible by i -> break 
├────────────┼────────────┼───────────────────────────┤
         10           2  x divisible by i -> break 
└────────────┴────────────┴───────────────────────────┘

 

Feherke.
feherke.github.io

RE: Sum of all primes numbers

(OP)
After I analyzed again what you told me I understand, thank you so much for taking time to explain me !!!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close