8000 [ADD] sum all primes added · luisprooc/js-algorithms@0210050 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0210050

Browse files
committed
[ADD] sum all primes added
1 parent 48ce7f3 commit 0210050

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,4 +611,20 @@ sumFibs(75024) should return 60696.
611611
612612
sumFibs(75025) should return 135721.
613613
614+
```
615+
616+
617+
## Sum All Primes
618+
619+
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.
620+
621+
Rewrite sumPrimes so it returns the sum of all prime numbers that are less than or equal to num.
622+
623+
```javascript
624+
sumPrimes(10) should return a number.
625+
626+
sumPrimes(10) should return 17.
627+
628+
sumPrimes(977) should return 73156.
629+
614630
```

src/sumPrimes.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function sumPrimes(num) {
2+
let sum = 0;
3+
let prime = false;
4+
for(let i = 2; i <= num; ++i){
5+
for(let j = 1; j < i; ++j){
6+
if(i % j === 0 && j > 1){
7+
prime = false;
8+
break;
9+
}
10+
else{
11+
prime = true;
12+
}
13+
}
14+
if(prime){
15+
sum += i;
16+
}
17+
}
18+
return sum;
19+
}
20+
21+
console.log(sumPrimes(977));

0 commit comments

Comments
 (0)
0