npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@sigma-js/primes

v1.0.3

Published

JavaScript library for efficient prime number detection and arithmetic, with customizable algorithms for large numbers.

Downloads

36

Readme

@sigma-js/primes NPM version NPM license NPM monthly downloads NPM total downloads

@sigma-js/primes is a javascript library that enables fast detection of prime numbers and prime number arithmetic with the following characteristics.

  • The default setting uses the Sieve of Eratosthenes to generate a list of prime numbers ranging from 1 to 8388607.
  • The generated prime number list is used for prime number detection and prime number arithmetic, allowing for high-speed processing.
  • However, if the target prime number is larger than the default setting, the processing time for generating prime numbers may become longer. In this case, the range of prime numbers to be generated can be narrowed down or an appropriate algorithm (Eratosthenes sieve or Atkin sieve) can be set according to the range of prime numbers to speed up the process of prime number generation even for large numbers.

Install

Install with npm:

$ npm i @sigma-js/primes

Usage

JavaScript require

const { isPrime, isCoprime, getPrimes … } = require("@sigma-js/primes");

TypeScript import

import { isPrime, isCoprime, getPrimes … } from "@sigma-js/primes";

Call example: init

// You can default settings can be changed
init({ sieveType: "atkin" }); // default: { sieveType: "eratosthenes" } -> after setting: { sieveType: "atkin" }
init({ minNum: 1234567 }); // default: { minNum: 1 } -> after setting: { minNum: 1234567 }
init({ maxNum: 8901234 }); // default: { maxNum: 8388607 } -> after setting: { maxNum: 8901234 }
init({ sieveType: "atkin", minNum: 1234567, maxNum: 8901234 }); // set all at once

Call example: isPrime

// You can get the result if the specified number is prime.
isPrime(3); // Output: true
isPrime(8); // Output: false
isPrime(8388607); // Output: false

Call example: isCoprime

// You can get the result if the given numbers are coprime.
isCoprime(15, 28); // Output: true
isCoprime(8, 12); // Output: false

Call example: getPrimes

// You can specify a range and get the prime numbers in that range.
init({ minNum: 100, maxNum: 150 }); // Set maximum value to 100 and maximum value to 150
getPrimes(); // Range: 100~150, Output: [101, 103, 107, 109, 113, 127, 131, 137, 139, 149]
getPrimes(125); // Range: 125~150, Output: [127, 131, 137, 139, 149]
getPrimes(110, 140); // Range: 110~140, Output: [113, 127, 131, 137, 139]

Call example: getPrimesAverage

// You can get the average of prime numbers within a specified range.
init({ maxNum: 30 }); // Set the maximum value to 30
getPrimesAverage(); // Range: 1~30, Rounded to third decimal places, Output: 12.9
getPrimesAverage(25); // Range: 25~30, Rounded to third decimal places, Output: 29
getPrimesAverage(5, 30); // Range: 5~30, Rounded to third decimal places, Output: 15.5
getPrimesAverage(1, 5, 9); // Range: 1~5, Rounded to the ninth decimal place, Output: 3.333333333
getPrimesAverage(5, 12, 9); // Range: 5~12, Rounded to the ninth decimal place, Output: 7.666666667
getPrimesAverage(5, 12, 0); // Range: 5~12, Rounded to the first decimal place, Output: 8

Call example: getPrimesCount

// You can get the number of prime numbers in a specified range.
init({ maxNum: 20 }); // Set the maximum value to 20
getPrimesCount(); // Range: 1~20, Output: 8
getPrimesCount(6); // Range: 6~20, Output: 5
getPrimesCount(1, 10); // Range: 1~10, Output: 4

Call example: getPrimesIndex

// You can get the correct index of prime numbers in a given range.
init({ maxNum: 25 }); // Set the maximum value to 25
getPrimesIndex(3); // Range: 1~25, Output: 2
getPrimesIndex(17, 10); // Range: 10~25, Output: 3
getPrimesIndex(2, 1, 10); // Range: 1~10, Output: 1

Call example: getPrimesMedian

// You can get the number of prime numbers in a specified range.
init({ maxNum: 100 }); // Set the maximum value to 100
getPrimesMedian(); // Range: 1~100, Output: 41
getPrimesMedian(25); // Range: 25~100, Output: 60
getPrimesMedian(5, 34); // Range: 5~34, Output: 17
getPrimesMedian(5, 50); // Range: 5~50, Output: 23

Call example: getPrimesSum

// You can get the sum of prime numbers within a specified range.
init({ maxNum: 30 }); // Set the maximum value to 30
getPrimesSum(); // Range: 1~30, Output: 129
getPrimesSum(25); // Range: 25~30, Output: 29
getPrimesSum(5, 30); // Range: 5~30, Output: 124

Call example: getPrimesTwins

// You can get twin prime numbers within a specified range.
init({ maxNum: 20 }); // Set the maximum value to 20
getPrimesTwins(); // Range: 1~20, Output: [[3, 5],[5, 7],[11, 13],[17, 19]]
getPrimesTwins(6); // Range: 6~20, Output: [[11, 13],[17, 19]]
getPrimesTwins(1, 10); // Range: 1~10, Output: [[3, 5],[5, 7]]
getPrimesTwins(3, 4); // Range: 3~4, Output: []

Call example: getFactors

// You can obtain the prime factorization result of the specified number.
getFactors(24); // Output: [2, 2, 2, 3]
getFactors(555); // Output: [3, 5, 37]

Call example: getFactorsFormula

// You can get the prime factorization result of the specified number using a formula.
getFactorsFormula(24); // Output: "2^3*3"
getFactorsFormula(30); // Output: "2*3*5"
getFactorsFormula(7); // Output: "7"

Call example: getMultInverse

// You can get the multiplicative inverse of a given number.
getMultInverse(3, 11); // Output: 4 (Get x that becomes 3*x≡1(mod 11))
getMultInverse(7, 13); // Output: 2 (Get x that becomes 7*x≡1(mod 13))

Call example: getRandomPrime

// You can get prime numbers randomly within a specified range.
init({ maxNum: 100 }); // Set the maximum value to 100
getRandomPrime(); // Range: 1~100, Output: 7
getRandomPrime(10); // Range: 10~100, Output: 11
getRandomPrime(4, 12); // Range: 4~12, Output: 5

License

Copyright (c) 2023, s-hama. Released under the MIT License.