Created
December 3, 2015 02:45
-
-
Save anonymous/2e998ba11c338c6752e4 to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/nirajkrz 's solution for Bonfire: Sum All Primes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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