Skip to content

Instantly share code, notes, and snippets.

@jwill9999
Created October 10, 2017 16:41
Show Gist options
  • Select an option

  • Save jwill9999/7ae75a884ae89e1dc716f594ff6138a0 to your computer and use it in GitHub Desktop.

Select an option

Save jwill9999/7ae75a884ae89e1dc716f594ff6138a0 to your computer and use it in GitHub Desktop.
Sum All Primes
/*
Sum all the prime numbers up to and including the provided number.
A prime number is defined as a number greater than one and having only two divisors, one and itself. For example, 2 is a prime number because it's only divisible by one and two.
The provided number may not be a prime.
*/
function sumPrimes(num) {
var primes = [];
for (var j = 2; j <= num; j++) {
if (isPrimeNumber(j)) {
primes.push(j);
}
}
function isPrimeNumber(num) {
for (var i = 2; i < num; i++) {
if (num % i === 0) {
return false;
}
}
return true;
}
console.log(primes);
return primes.reduce(function(a, b) {
return a + b;
});
}
sumPrimes(977);
/*
test your code here
sumPrimes(10) should return a number.
sumPrimes(10) should return 17.
sumPrimes(977) should return 73156.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment