ts-x-prime
v0.1.0
Published
Library for working with prime numbers
Readme
ts-x-prime
A lightweight TypeScript library for working with prime numbers: generate primes, test for primality, and use the AKS primality test.
Installation
npm install ts-x-primeor with pnpm:
pnpm add ts-x-primeor with yarn:
yarn add ts-x-primeFeatures
| Function | Description |
| ----------------------------- | ---------------------------------------------------------------- |
| generateSieve(n) | Generates a boolean sieve of Eratosthenes up to n |
| sieveToPrimes(s) | Converts a sieve to a list of primes |
| isPrimeSieve(n, s) | Checks if n is prime using a precomputed sieve |
| isPrime(n) | Checks if n is prime using the 6k ± 1 optimization |
| isPrimeAKS(n) | Checks if n is prime using the AKS primality test (slow) |
| zetaReal(s, terms) | Approximates the Riemann zeta function for real s |
| zetaEuler(s, max) | Evaluates ζ(s) via the Euler product using primes up to max |
| gamma(z) | Computes the gamma function for a complex number z |
| eta(s, terms) | Dirichlet eta function η(s) for complex s with specified terms |
| zeta(s, terms) | Approximates ζ(s) for complex s using analytic continuation |
| new Goldbach(n).getPairs() | Getting goldbach pairs for n |
| new Goldbach(n).getPrimes() | Getting goldbach primes for n |
Usage Examples
import {
generateSieve,
sieveToPrimes,
isPrimeSieve,
isPrime,
isPrimeAKS,
zetaReal,
zetaEuler,
gamma,
eta,
zeta,
Complex,
Goldbach,
} from 'ts-x-prime'
// Generate primes using sieve
const sieve = generateSieve(100)
const primes = sieveToPrimes(sieve) // [2, 3, 5, 7, 11, ..., 97]
// Check using sieve
console.log(isPrimeSieve(37, sieve)) // true
console.log(isPrimeSieve(40, sieve)) // false
// Check using 6k ± 1 method
console.log(isPrime(29)) // true
console.log(isPrime(35)) // false
// Check using AKS (slow)
console.log(isPrimeAKS(13)) // true
console.log(isPrimeAKS(15)) // false
// Real zeta function
console.log(zetaReal(2)) // ~1.6449 (π²/6)
// Zeta via Euler product
console.log(zetaEuler(2)) // ~1.6449
// Gamma function
console.log(gamma(new Complex(0.5, 0))) // ~1.772 (√π)
// Dirichlet eta
console.log(eta(new Complex(1, 0), 10000)) // ~ln(2)
// Full zeta function (complex)
console.log(zeta(new Complex(2, 0))) // ~1.6449
// Getting goldbach pairs for 10
console.log(new Goldbach(10).getPairs()) // [[3, 7], [5, 5]]Notes
isPrimeAKSuses the deterministic AKS algorithm and is intended for theoretical or educational use. It's very slow for large inputs.isPrimeuses the 6k ± 1 method, which is efficient for medium-sized numbers.zetaReal,zetaEuler,zeta,eta, andgammaare provided for exploration of analytic number theory.- For large batches of prime checks, prefer generating a sieve first.
License
MIT © rekmixa
