Skip to content

Instantly share code, notes, and snippets.

@rfprod
Last active April 22, 2017 15:58
Show Gist options
  • Save rfprod/b46037cf79127e5854d4 to your computer and use it in GitHub Desktop.
Save rfprod/b46037cf79127e5854d4 to your computer and use it in GitHub Desktop.
Sum All Primes
function sumPrimes(num) {
var sum = 0;
var div = 0;
var divCounter = 0;
for (var i=2;i<=num;i++){
if (isPrime(i) === true){
sum = sum + i;
}
}
function isPrime(n){
var isDivisible = false;
divCounter = 0;
for (var j=1;j<=n;j++){
div = Math.abs(n%j);
if (div > 0){
divCounter = divCounter;
}else{
divCounter = divCounter + 1;
}
}
if (divCounter <= 2){
return true;
}else{
return false;
}
return false;
}
// return isPrime(num);
return sum;
}
sumPrimes(977);

Sum All Primes

Sums all the prime numbers up to and including the provided number. A prime number is defined as having only two divisors, 1 and itself. For example, 2 is a prime number because it's only divisible by 1 and 2. 1 isn't a prime number, because it's only divisible by itself. The provided number may not be a prime.

A script by V.

License.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment