Skip to content

Instantly share code, notes, and snippets.

@nichanank
Created April 6, 2020 14:37
Show Gist options
  • Save nichanank/8c099f79b505864c48e2fb3c01ae798b to your computer and use it in GitHub Desktop.
Save nichanank/8c099f79b505864c48e2fb3c01ae798b to your computer and use it in GitHub Desktop.
// from FCC
function sumPrimes(num) {
var numberList = []
for (var i = 2; i <= num; i++) {
numberList.push(i)
}
let nonPrimes = []
for (var i = 0; i < numberList.length; i++) {
var p = numberList[i]
for (var j = p; j <= num; j+=p) {
if (j % p === 0 && j !== p) {
nonPrimes.push(j)
}
}
}
let primes = numberList.filter(x => !nonPrimes.includes(x))
return primes.reduce((sum,prime) => sum + prime, 0)
}
sumPrimes(10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment