Skip to content

Instantly share code, notes, and snippets.

@greemwahr
Created November 1, 2015 02:21
Show Gist options
  • Save greemwahr/5850381e5a02650cd560 to your computer and use it in GitHub Desktop.
Save greemwahr/5850381e5a02650cd560 to your computer and use it in GitHub Desktop.
codeSnippet
function isPrime(num) {
var absNum = Math.abs(num);
if (absNum <= 1) {
return false;
} else if (absNum <= 3) {
return true;
} else if (absNum % 2 === 0 || absNum % 3 === 0) {
return false;
}
// The while loop takes into account numbers divisible by 5, 7, 11, 13 and 23
var i = 5;
while (i*i <= absNum) {
if (absNum % i === 0 || absNum % (i + 2) === 0) return false;
i += 6;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment