Skip to content

Instantly share code, notes, and snippets.

@1995eaton
Created July 3, 2016 14:03
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 1995eaton/88ac18de361047113ddd15b3d4565b09 to your computer and use it in GitHub Desktop.
Save 1995eaton/88ac18de361047113ddd15b3d4565b09 to your computer and use it in GitHub Desktop.
Regex prime test
function isPrime(n) {
if (n <= 1)
return false;
var pattern = new RegExp('^(.{2,' + Math.ceil(Math.sqrt(n)) + '})\\1+$');
return !pattern.test('.'.repeat(n));
}
var n = ~~process.argv[2];
console.log(`${n} ${isPrime(n) ? 'is' : 'is not'} prime`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment