Created
September 1, 2018 17:26
-
-
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.
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
#!/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