Skip to content

Instantly share code, notes, and snippets.

Created December 3, 2015 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/2e998ba11c338c6752e4 to your computer and use it in GitHub Desktop.
Save anonymous/2e998ba11c338c6752e4 to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/nirajkrz 's solution for Bonfire: Sum All Primes
// Bonfire: Sum All Primes
// Author: @nirajkrz
// Challenge: http://www.freecodecamp.com/challenges/bonfire-sum-all-primes
// Learn to Code at Free Code Camp (www.freecodecamp.com)
//Sum all the prime numbers up to and including the provided number.
function sumPrimes(num) {
var res = 0;
// FUnction to get the primes up to max in an array
function getPrimes(max) {
var sieve = [];
var i;
var j;
var primes = [];
for (i = 2; i <= max; ++i) {
if (!sieve[i]) {
// i has not been marked -- it is prime
primes.push(i);
for (j = i << 1; j <= max; j += i) {
sieve[j] = true;
}
}
}
return primes;
}
// Add the primes
var primes = getPrimes(num);
for (var p = 0; p < primes.length; p++) {
res += primes[p];
}
return res;
}
sumPrimes(10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment