Created
December 9, 2015 14:05
-
-
Save anonymous/de57a0110ef2ae087f98 to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/patrickcurl 's solution for Bonfire: Sum All Primes
This file contains hidden or 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: @patrickcurl | |
// Challenge: http://www.freecodecamp.com/challenges/bonfire-sum-all-primes | |
// Learn to Code at Free Code Camp (www.freecodecamp.com) | |
function sumPrimes(num) { | |
if(num === 1){ | |
return 0; | |
} | |
if(isPrime(num) === false){ | |
return sumPrimes(num-1); | |
} | |
if(isPrime(num) === true){ | |
return num + sumPrimes(num-1); | |
} | |
} | |
function isPrime(num){ | |
for(i=2; i<= num;i++){ | |
if(num % i === 0 && num != i){ | |
return false; | |
} | |
} | |
return true; | |
} | |
sumPrimes(10); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment