Skip to content

Instantly share code, notes, and snippets.

@hendriklammers
Last active December 15, 2015 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hendriklammers/5221278 to your computer and use it in GitHub Desktop.
Save hendriklammers/5221278 to your computer and use it in GitHub Desktop.
Javascript: isPrimeNumber checks whether given value is a prime number or not
/**
* Use isPrimeNumber to check whether a number is a Prime number or not.
* @param {Number} value The number to check
* @return {Boolean} Is the given value a Prime Number or not.
*/
function isPrimeNumber (value) {
var max = Math.sqrt(value);
// Do some checks to prevent the loop from running for numbers that are obvious
if (isNaN(value) || !isFinite(value) || value % 1 || value < 2) {
return false;
}
if (value % 2 === 0) {
return (value === 2);
}
if (value % 3 === 0) {
return (value === 3);
}
// Go through posible divisions
for (var i = 5; i <= max; i += 6) {
if (value % i === 0) {
return false;
}
if (value % (i + 2) === 0) {
return false;
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment