Skip to content

Instantly share code, notes, and snippets.

@mohit
Created January 29, 2014 19:58
Show Gist options
  • Save mohit/8695726 to your computer and use it in GitHub Desktop.
Save mohit/8695726 to your computer and use it in GitHub Desktop.
var primeTotal = function(){
var limit = 2000000;
var result = 0;
for (var i = 2; i <= limit; i++){
if (primeTester(i)){
result += i;
}
}
return result;
}
var primeTester = function(n) {
// n isn't a number or n is less than 1 or n is not an integer
if(typeof n !== 'number' || n < 2 || n % 1 !== 0){
return false;
}
// if n is 2
if (n === 2) return true;
// if n is divisible by 2
if (n%2 === 0) return false;
var sqrt = Math.ceil(Math.sqrt(n));
for(var i=3; i<=sqrt; i+=2){
if (n%i === 0) return false
}
return true;
};
console.log(primeTotal());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment