Skip to content

Instantly share code, notes, and snippets.

@TheIronDev
Created July 19, 2014 20:10
Show Gist options
  • Save TheIronDev/26aa2bc442a99d16a7fc to your computer and use it in GitHub Desktop.
Save TheIronDev/26aa2bc442a99d16a7fc to your computer and use it in GitHub Desktop.
/**
* 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