Created
November 1, 2015 02:21
-
-
Save greemwahr/5850381e5a02650cd560 to your computer and use it in GitHub Desktop.
codeSnippet
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
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