Created
July 19, 2014 20:10
-
-
Save TheIronDev/26aa2bc442a99d16a7fc to your computer and use it in GitHub Desktop.
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
/** | |
* To Execute, run: node isPrime 13 | |
* | |
*/ | |
var testNumber = process.argv[2]; | |
/** | |
* @param test - an integer between 1 and 2^16 | |
* If the number is negative, we will absolute value it. | |
* We are also assuming 1 is not a prime number. | |
* @returns {boolean} | |
*/ | |
function isPrime (test) { | |
test = Math.abs(test); | |
if (!(test-1)) return false; | |
var max = Math.floor(test/2); | |
while (max>1) { | |
if (test%max === 0) return false; | |
max--; | |
} | |
return true; | |
} | |
console.log(isPrime(testNumber)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment