Skip to content

Instantly share code, notes, and snippets.

@drbr
Created September 1, 2018 17:26
Show Gist options
  • Save drbr/9bddd960f853a7d7805abd852e3dc80e to your computer and use it in GitHub Desktop.
Save drbr/9bddd960f853a7d7805abd852e3dc80e to your computer and use it in GitHub Desktop.
Given a positive integer given as a command line argument, determines if that number is prime.
#!/usr/bin/env node
const process = require('process');
const num = parseInt(process.argv[2], 10);
const CompositeRegex = /^(..+)\1+$/;
function isPrime(num) {
const str = new Array(num + 1).join(' ');
return !CompositeRegex.test(str);
}
const prime = isPrime(num);
if (prime) {
console.log('prime');
} else {
console.log('not prime');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment