Skip to content

Instantly share code, notes, and snippets.

@DekusDenial
Created June 17, 2013 05:00
Show Gist options
  • Save DekusDenial/5794737 to your computer and use it in GitHub Desktop.
Save DekusDenial/5794737 to your computer and use it in GitHub Desktop.
Prime Number
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