Last active
March 16, 2019 00:20
-
-
Save reqshark/d145c251bfe6d9c0941ca90f6b34d640 to your computer and use it in GitHub Desktop.
summing primes from 2 million
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
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