is-prime-number
Advanced tools
Comparing version 2.0.0 to 2.0.1
23
index.js
/** | ||
* Check if number provided is a prime number | ||
* @param {int} num | ||
* @return {boolean} number is prime | ||
*/ | ||
const isPrime = (num) => { | ||
if (!Number.isInteger(num)) | ||
return false; | ||
// Prime numbers are whole, non-negative numbers | ||
// 1 is a composite number (not prime as it's only divisible by itself) | ||
if (num < 2) | ||
return false; | ||
// 2 is the first prime number | ||
if (num === 2) | ||
return true; | ||
for (let i = 2; i < num; i++) { | ||
// Excluding 2 no even number can be prime | ||
if (num % 2 === 0) | ||
return false; | ||
// Run divisibility tests up to and including it's square root | ||
const sqrt = Math.sqrt(num); | ||
for (let i = 3; i <= sqrt; i++) { | ||
if(num % i === 0) | ||
return false; | ||
} | ||
return true; | ||
}; | ||
return num > 1; | ||
} | ||
module.exports = isPrime; |
{ | ||
"name": "is-prime-number", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "Returns boolean of if provided number is prime.", | ||
@@ -22,3 +22,6 @@ "main": "index.js", | ||
}, | ||
"homepage": "https://github.com/bradleyflood/is-prime-number#readme" | ||
"homepage": "https://github.com/bradleyflood/is-prime-number#readme", | ||
"devDependencies": { | ||
"tap": "^10.7.2" | ||
} | ||
} |
21
test.js
@@ -0,15 +1,12 @@ | ||
const tap = require('tap'); | ||
const isPrime = require('./index.js'); | ||
if (! isPrime(3)) | ||
process.exit(1); | ||
const primes = [2,3,7,11,103,197,199]; | ||
primes.forEach((i) => { | ||
tap.ok(isPrime(i)); | ||
}); | ||
if (! isPrime(7)) | ||
process.exit(1); | ||
if (isPrime(1)) | ||
process.exit(1); | ||
if (isPrime(4)) | ||
process.exit(1); | ||
process.exit(0); | ||
const nonPrimes = [1,4,100,'foo',true, {bar:1}, []]; | ||
nonPrimes.forEach((i) => { | ||
tap.notOk(isPrime(i)); | ||
}); |
Sorry, the diff of this file is not supported yet
Possible typosquat attack
Supply chain riskThere is a package with a similar name that is downloaded much more often.
Did you mean |
---|
is-number |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10
37
14429
1
1