Created
January 29, 2014 19:58
-
-
Save mohit/8695726 to your computer and use it in GitHub Desktop.
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
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