Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sum of all primes numbers

Status
Not open for further replies.

CrxC01

Technical User
Mar 25, 2021
5
CH
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!

 
Hi

I do not get you. For [tt]sumPrimes[teal]([/teal][purple]10[/purple][teal])[/teal][/tt] 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
 
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...
 
Hi

CrxC01 said:
(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 [tt]else if (i===x-1)[/tt] line is not executed. It would be executed when i = 3, but it [tt]break[/tt]s out from the [tt]for[/tt] loop when i = 2.

Here I added some [tt]console.log()[/tt]s to your code :
Code:
[b]function[/b] [COLOR=orange]sumPrimes[/color][teal]([/teal]num[teal])[/teal]
[teal]{[/teal]
	[b]var[/b] x [teal]=[/teal] [purple]2[/purple][teal];[/teal]
	[b]var[/b] added [teal]=[/teal] [purple]2[/purple][teal];[/teal]
	[b]while[/b] [teal]([/teal]x [teal]<[/teal] num[teal]) {[/teal]
		x[teal]++;[/teal]
		[b]for[/b] [teal]([/teal][b]var[/b] i [teal]=[/teal] [purple]2[/purple][teal];[/teal] i [teal]<[/teal] x[teal];[/teal] i[teal]++) {[/teal]
			[b]if[/b] [teal]([/teal]x [teal]%[/teal] i [teal]===[/teal] [purple]0[/purple][teal]) {[/teal]
console[teal].[/teal][COLOR=orange]log[/color][teal]([/teal][i][green]'skipped'[/green][/i][teal],[/teal] x[teal],[/teal] [i][green]'because divisible with'[/green][/i][teal],[/teal] i[teal])[/teal]
				[b]break[/b][teal];[/teal]
			[teal]}[/teal] [b]else if[/b] [teal]([/teal]i [teal]===[/teal] x [teal]-[/teal] [purple]1[/purple][teal]) {[/teal]
console[teal].[/teal][COLOR=orange]log[/color][teal]([/teal][i][green]'ADDED  '[/green][/i][teal],[/teal] x[teal])[/teal]
				added [teal]+=[/teal] x[teal];[/teal]
			[teal]}[/teal]
		[teal]}[/teal]
	[teal]}[/teal]
	[b]return[/b] added[teal];[/teal]
[teal]}[/teal]

console[teal].[/teal][COLOR=orange]log[/color][teal]([/teal][i][green]'Result '[/green][/i][teal],[/teal] [COLOR=orange]sumPrimes[/color][teal]([/teal][purple]10[/purple][teal]));[/teal]

Then when running it produces this output :
Code:
[navy]master #[/navy] 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
 
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.
 
Hi

Unsure what exactly are you not understanding there.

There are 2 loops : the outer [tt]while[/tt] loop and the inner [tt]for[/tt] loop :
[ul]
[li]The [tt]while[/tt] loop iterates once over the values from 2 to num - 1[/li]
[li]The [tt]for[/tt] loop iterates once for each x value over the values from 2 to x - 1[/li]
[/ul]

[pre]
[silver]╔════════════╦════════════════════════════════════════╗[/silver]
[silver]║[/silver] while loop [silver]║[/silver] for loop [silver]║[/silver]
[silver]║[/silver] value of x [silver]║[/silver] value of i [silver]║[/silver] condition met [silver]║[/silver]
[silver]╠════════════╬════════════╬═══════════════════════════╣[/silver]
[silver]│[/silver] 2 [silver]│[/silver] 2 [silver]│[/silver] x divisible by i -> break [silver]│[/silver]
[silver]├────────────┼────────────┼───────────────────────────┤[/silver]
[silver]│[/silver] 3 [silver]│[/silver] 2 [silver]│[/silver] last i tested -> is prime [silver]│[/silver]
[silver]├────────────┼────────────┼───────────────────────────┤[/silver]
[silver]│[/silver] 4 [silver]│[/silver] 2 [silver]│[/silver] x divisible by i -> break [silver]│[/silver]
[silver]├────────────┼────────────┼───────────────────────────┤[/silver]
[silver]│[/silver] 5 [silver]│[/silver] 2 [silver]│[/silver] [silver]│[/silver]
[silver]│[/silver] [silver]│[/silver] 3 [silver]│[/silver] [silver]│[/silver]
[silver]│[/silver] [silver]│[/silver] 4 [silver]│[/silver] last i tested -> is prime [silver]│[/silver]
[silver]├────────────┼────────────┼───────────────────────────┤[/silver]
[silver]│[/silver] 6 [silver]│[/silver] 2 [silver]│[/silver] x divisible by i -> break [silver]│[/silver]
[silver]├────────────┼────────────┼───────────────────────────┤[/silver]
[silver]│[/silver] 7 [silver]│[/silver] 2 [silver]│[/silver] [silver]│[/silver]
[silver]│[/silver] [silver]│[/silver] 3 [silver]│[/silver] [silver]│[/silver]
[silver]│[/silver] [silver]│[/silver] 4 [silver]│[/silver] [silver]│[/silver]
[silver]│[/silver] [silver]│[/silver] 5 [silver]│[/silver] [silver]│[/silver]
[silver]│[/silver] [silver]│[/silver] 6 [silver]│[/silver] last i tested -> is prime [silver]│[/silver]
[silver]├────────────┼────────────┼───────────────────────────┤[/silver]
[silver]│[/silver] 8 [silver]│[/silver] 2 [silver]│[/silver] x divisible by i -> break [silver]│[/silver]
[silver]├────────────┼────────────┼───────────────────────────┤[/silver]
[silver]│[/silver] 9 [silver]│[/silver] 2 [silver]│[/silver] [silver]│[/silver]
[silver]│[/silver] [silver]│[/silver] 3 [silver]│[/silver] x divisible by i -> break [silver]│[/silver]
[silver]├────────────┼────────────┼───────────────────────────┤[/silver]
[silver]│[/silver] 10 [silver]│[/silver] 2 [silver]│[/silver] x divisible by i -> break [silver]│[/silver]
[silver]└────────────┴────────────┴───────────────────────────┘[/silver]

[/pre]

Feherke.
feherke.github.io
 
After I analyzed again what you told me I understand, thank you so much for taking time to explain me !!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top