Utils module - Helper functions for the UOR Math-JS library Implements advanced mathematical utilities optimized for the Prime Framework

Classes

PrimeMathError
PrimeMathError

Members

(inner, constant) primeCache

Export the prime cache for external use Provides access to the cache for statistics and management

Methods

(inner) exactDivide(dividend, divisor) → {BigInt}

Perform exact division, ensuring the result is an integer Aligns with the Prime Framework's requirement for exact arithmetic

Parameters:
NameTypeDescription
dividendBigInt

The number to divide

divisorBigInt

The divisor

Throws:

If the division is not exact or if divisor is zero

Type
PrimeMathError
Returns:

The result of the division

Type: 
BigInt

(inner) factorial(n) → {BigInt}

Factorial function Optimized for the Prime Framework

Parameters:
NameTypeDescription
nBigInt | number

The number to calculate factorial for

Throws:

If n is negative

Type
PrimeMathError
Returns:

n!

Type: 
BigInt

(inner) fastExp(base, exponent, modulusopt) → {BigInt}

Fast exponentiation algorithm (exponentiation by squaring) Efficiently computes base^exponent in O(log n) time Optimized for use in the Prime Framework Enhanced to handle large exponents safely

Parameters:
NameTypeAttributesDefaultDescription
baseBigInt

The base value

exponentBigInt

The exponent value (must be non-negative)

modulusBigInt<optional>
null

Optional modulus for modular exponentiation

Throws:

If exponent is negative

Type
PrimeMathError
Returns:

base raised to the power of exponent (modulo modulus if provided)

Type: 
BigInt

(inner) gcd(a, b) → {BigInt}

Calculate the greatest common divisor (GCD) of two numbers using the binary GCD algorithm Optimized version of the Euclidean algorithm for better performance in the Prime Framework

Parameters:
NameTypeDescription
aBigInt

First number

bBigInt

Second number

Returns:

The GCD of a and b

Type: 
BigInt

(inner) getNthPrime(n) → {BigInt}

Get the nth prime number Uses optimized caching and generation for efficiency

Parameters:
NameTypeDescription
nBigInt | number | string

The 1-based index of the prime number to retrieve

Throws:

If n is not a positive integer

Type
PrimeMathError
Returns:

The nth prime number

Type: 
BigInt

(inner) getPrimeRange(start, end, optionsopt) → {Array.<BigInt>}

Get a range of prime numbers Efficiently generates primes within a specified range Uses segmented sieve of Eratosthenes for optimal performance Enhanced to respect configurable limits

Parameters:
NameTypeAttributesDescription
startBigInt | number

The lower bound of the range (inclusive)

endBigInt | number

The upper bound of the range (inclusive)

optionsObject<optional>

Options for prime generation

Properties
NameTypeAttributesDescription
segmentSizeBigInt | number<optional>

Size of each segment for the segmented sieve

dynamicboolean<optional>

Whether to use dynamic segment sizing (overrides config)

maxCountnumber<optional>

Maximum number of primes to return

Throws:

If parameters are invalid

Type
PrimeMathError
Returns:

Array of prime numbers in the specified range

Type: 
Array.<BigInt>

(inner) isDivisible(num, divisor) → {boolean}

Check if a number is divisible by another

Parameters:
NameTypeDescription
numBigInt

The number to check

divisorBigInt

The potential divisor

Throws:

If divisor is zero

Type
PrimeMathError
Returns:

True if num is divisible by divisor, false otherwise

Type: 
boolean

(inner) isMersennePrime(n) → {boolean}

Check if a number is a Mersenne prime (a prime of the form 2^n - 1) Mersenne primes have the form 2^p - 1 where p is also prime

Parameters:
NameTypeDescription
nBigInt | number | string

The number to check

Returns:

True if n is a Mersenne prime, false otherwise

Type: 
boolean

(inner) isPrime(n, optionsopt) → {boolean}

Parameters:
NameTypeAttributesDescription
nBigInt | number | string

The number to check for primality

optionsPrimeTestOptions<optional>

Options for primality testing

Returns:

True if n is prime, false otherwise

Type: 
boolean

(inner) lcm(a, b) → {BigInt}

Calculate the least common multiple (LCM) of two numbers Optimized for the Prime Framework using the GCD

Parameters:
NameTypeDescription
aBigInt

First number

bBigInt

Second number

Returns:

The LCM of a and b

Type: 
BigInt

(inner) moebiusFunction(n) → {BigInt}

Calculate the Möbius function μ(n) value for a number The Möbius function is defined as: μ(n) = 1 if n is square-free with an even number of prime factors -1 if n is square-free with an odd number of prime factors 0 if n has a squared prime factor

Parameters:
NameTypeDescription
nBigInt | number | string

The number to compute the Möbius function for

Throws:

If n is not a positive integer

Type
PrimeMathError
Returns:

The Möbius function value

Type: 
BigInt

(inner) nextPrime(n) → {BigInt}

Get the next prime number after a given number Enhanced with prime cache for efficiency

Parameters:
NameTypeDescription
nBigInt | number

The starting number

Returns:

The next prime number after n

Type: 
BigInt

(generator, inner) primeGenerator(options) → {Generator.<BigInt>}

Get a prime number generator/iterator that produces sequential primes Follows the Prime Framework's stream-based approach

Parameters:
NameTypeDescription
optionsObject

Generator options

Properties
NameTypeAttributesDefaultDescription
startBigInt | number<optional>
2

The number to start from (inclusive if prime)

endBigInt | number<optional>

Optional end value (inclusive)

countnumber<optional>

Optional max count of primes to generate

Returns:

A generator that yields prime numbers

Type: 
Generator.<BigInt>

(inner) quadraticResidue(a, p) → {boolean}

Check if a is a quadratic residue modulo p A number a is a quadratic residue modulo p if there exists an x such that x^2 ≡ a (mod p)

Parameters:
NameTypeDescription
aBigInt | number | string

The number to check

pBigInt | number | string

The prime modulus

Throws:

If p is not likely a prime

Type
PrimeMathError
Returns:

True if a is a quadratic residue modulo p, false otherwise

Type: 
boolean

(inner) toBigInt(value) → {BigInt}

Safely convert a value to BigInt Enhanced to handle a wider range of inputs for the Prime Framework

Parameters:
NameTypeDescription
valuenumber | string | BigInt

The value to convert

Throws:

If the value cannot be converted to BigInt

Type
PrimeMathError
Returns:

The value as a BigInt

Type: 
BigInt

Type Definitions

PrimeTestOptions

Type:
  • Object
Properties
NameTypeDescription
useCacheboolean

Whether to use the prime cache

updateCacheboolean

Whether to update the cache with result