Utils module - Helper functions for the UOR Math-JS library Implements advanced mathematical utilities optimized for the Prime Framework
Classes
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
Name | Type | Description |
---|---|---|
dividend | BigInt | The number to divide |
divisor | BigInt | The divisor |
If the division is not exact or if divisor is zero
- Type
- PrimeMathError
The result of the division
- Type:
- BigInt
(inner) factorial(n) → {BigInt}
Factorial function Optimized for the Prime Framework
Name | Type | Description |
---|---|---|
n | BigInt | | The number to calculate factorial for |
If n is negative
- Type
- PrimeMathError
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
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
base | BigInt | The base value | ||
exponent | BigInt | The exponent value (must be non-negative) | ||
modulus | BigInt | <optional> | null | Optional modulus for modular exponentiation |
If exponent is negative
- Type
- PrimeMathError
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
Name | Type | Description |
---|---|---|
a | BigInt | First number |
b | BigInt | Second number |
The GCD of a and b
- Type:
- BigInt
(inner) getNthPrime(n) → {BigInt}
Get the nth prime number Uses optimized caching and generation for efficiency
Name | Type | Description |
---|---|---|
n | BigInt | | The 1-based index of the prime number to retrieve |
If n is not a positive integer
- Type
- PrimeMathError
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
Name | Type | Attributes | Description | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
start | BigInt | | The lower bound of the range (inclusive) | |||||||||||||||||
end | BigInt | | The upper bound of the range (inclusive) | |||||||||||||||||
options | Object | <optional> | Options for prime generation Properties
|
If parameters are invalid
- Type
- PrimeMathError
Array of prime numbers in the specified range
- Type:
- Array.<BigInt>
(inner) isDivisible(num, divisor) → {boolean}
Check if a number is divisible by another
Name | Type | Description |
---|---|---|
num | BigInt | The number to check |
divisor | BigInt | The potential divisor |
If divisor is zero
- Type
- PrimeMathError
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
Name | Type | Description |
---|---|---|
n | BigInt | | The number to check |
True if n is a Mersenne prime, false otherwise
- Type:
- boolean
(inner) isPrime(n, optionsopt) → {boolean}
Name | Type | Attributes | Description |
---|---|---|---|
n | BigInt | | The number to check for primality | |
options | PrimeTestOptions | <optional> | Options for primality testing |
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
Name | Type | Description |
---|---|---|
a | BigInt | First number |
b | BigInt | Second number |
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
Name | Type | Description |
---|---|---|
n | BigInt | | The number to compute the Möbius function for |
If n is not a positive integer
- Type
- PrimeMathError
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
Name | Type | Description |
---|---|---|
n | BigInt | | The starting number |
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
Name | Type | Description | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options | Object | Generator options Properties
|
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)
Name | Type | Description |
---|---|---|
a | BigInt | | The number to check |
p | BigInt | | The prime modulus |
If p is not likely a prime
- Type
- PrimeMathError
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
Name | Type | Description |
---|---|---|
value | number | | The value to convert |
If the value cannot be converted to BigInt
- Type
- PrimeMathError
The value as a BigInt
- Type:
- BigInt
Type Definitions
PrimeTestOptions
- Object
Name | Type | Description |
---|---|---|
useCache | boolean | Whether to use the prime cache |
updateCache | boolean | Whether to update the cache with result |