Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

is-prime-number

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

is-prime-number - npm Package Compare versions

Comparing version 2.0.0 to 2.0.1

.idea/misc.xml

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;

7

package.json
{
"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"
}
}

@@ -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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc