Skip to content

Instantly share code, notes, and snippets.

@reqshark
Last active March 16, 2019 00:20
Show Gist options
  • Save reqshark/d145c251bfe6d9c0941ca90f6b34d640 to your computer and use it in GitHub Desktop.
Save reqshark/d145c251bfe6d9c0941ca90f6b34d640 to your computer and use it in GitHub Desktop.
summing primes from 2 million
console.log(primesum(2000000))
function primesum(m){
let ret = 7, i = 1 //(ret=7) prepopulate 2 and 5, dropped by optimization
while (i++ < m) {
if (isprime(i))
ret += i
}
return ret
function isprime(n){
let l = n
let preflight = Number(String(n).slice(-1))
if ( preflight === 5 || !(preflight%2) )
return false
while(l-- > 2)
if (!(n % l))
return false
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment