Created
June 17, 2013 05:00
-
-
Save DekusDenial/5794737 to your computer and use it in GitHub Desktop.
Prime Number
This file contains hidden or 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
function Prime() { | |
this.prime = 1; | |
} | |
Prime.prototype.isPrime = function(num) { | |
var result = true, maxToCheck = Math.ceil(Math.sqrt(num)); // integer number comparison is faster | |
if (num & 1) { // if odd number | |
for (var f = 3; f <= maxToCheck; f += 2) { | |
if (!(result = !!(num % f))) return false; | |
} | |
} else { // if even | |
return num === 2; | |
} | |
return result; | |
} | |
Prime.prototype.getNextPrime = function() { | |
// a prime number is not even, save one iteration for ++ | |
while (!this.isPrime(++this.prime)); | |
return this.prime; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment