Factorization module for the UOR Math-JS library Contains algorithms for converting integers into their prime factorization (universal coordinates) Enhanced with Prime Framework optimizations for performance and efficiency

Members

(inner, constant) factorizationCache

Prime Framework-specific factorization optimizations and utilities Provides specialized methods for the Prime Framework

Methods

(inner) ellipticCurveMethod(n, optionsopt) → {BigInt}

Lenstra's Elliptic Curve Method (ECM) for factorization Optimized for finding medium-sized factors of large numbers Enhanced with Prime Framework optimizations for universal coordinates

This implementation uses configurable parameters for number of curves, bounds, and memory limits. All limits can be set either through the options parameter or via the global configuration system in config.factorization.ecm.

Parameters:
NameTypeAttributesDescription
nBigInt

The number to factor

optionsObject<optional>

Algorithm options

Properties
NameTypeAttributesDescription
curvesnumber<optional>

Number of curves to try (default: config.factorization.ecm.maxCurves or scaled by number size)

b1number<optional>

Stage 1 bound (default: config.factorization.ecm.defaultB1)

b2number<optional>

Stage 2 bound (default: config.factorization.ecm.defaultB2 or b1*100 if 0)

maxMemorynumber<optional>

Max memory usage in MB (default: config.factorization.ecm.maxMemory or config.factorization.memoryLimit)

Throws:

If input is not a positive composite number

Type
PrimeMathError
Returns:

A non-trivial factor of n, or n if no factor is found

Type: 
BigInt

(inner) factorArrayToMap(factorArray) → {Map.<BigInt, BigInt>}

Convert array of {prime, exponent} objects to factorization map

Parameters:
NameTypeDescription
factorArrayArray.<PrimeFactor>

Array of prime-exponent objects

Returns:

Map of prime factors

Type: 
Map.<BigInt, BigInt>

(inner) factorMapToArray(factorMap) → {Array.<PrimeFactor>}

Convert factorization map to array of {prime, exponent} objects

Parameters:
NameTypeDescription
factorMapMap.<BigInt, BigInt>

Map of prime factors

Returns:

Array of prime-exponent objects

Type: 
Array.<PrimeFactor>

(inner) factorize(n) → {Map.<BigInt, BigInt>}

Factorize a number using trial division Implements Algorithm 1 from the specification for prime factorization

Parameters:
NameTypeDescription
nnumber | string | BigInt

The number to factorize

Throws:

If n is not a positive integer

Type
PrimeMathError
Returns:

A map where keys are prime factors and values are their exponents

Type: 
Map.<BigInt, BigInt>

(inner) factorizeOptimal(n, optionsopt) → {Map.<BigInt, BigInt>}

Factorize a number using the most appropriate algorithm based on its size and properties Enhanced with Prime Framework optimizations for better performance and precision Implements the Prime Framework's requirements for unique factorization and canonical form Uses configurable thresholds from config.factorization.thresholds to dynamically select the most efficient factorization algorithm based on number characteristics

Parameters:
NameTypeAttributesDescription
nnumber | string | BigInt

The number to factorize

optionsObject<optional>

Factorization options

Properties
NameTypeAttributesDefaultDescription
advancedboolean<optional>
false

Whether to use advanced factorization for large numbers

useCacheboolean<optional>
true

Whether to use factorization cache

parallelizeFactorizationboolean<optional>
false

Whether to use parallel factorization (when available)

algorithmParamsObject<optional>

Specific parameters for factorization algorithms

Properties
NameTypeAttributesDescription
ecmCurvesnumber<optional>

Number of curves for ECM

ecmB1number<optional>

B1 bound for ECM

ecmB2number<optional>

B2 bound for ECM (stage 2)

qsFactorBasenumber<optional>

Factor base size for quadratic sieve

qsSieveSizenumber<optional>

Sieve size for quadratic sieve

partialFactorizationboolean<optional>
false

Whether to allow partial factorization for very large numbers

validateFactorsboolean<optional>
true

Whether to validate that factors are indeed prime

Throws:

If n is not a positive integer

Type
PrimeMathError
Returns:

A map where keys are prime factors and values are their exponents

Type: 
Map.<BigInt, BigInt>

(inner) factorizeParallel(n, optionsopt) → {Map.<BigInt, BigInt>}

Implements parallel factorization using worker threads when available Falls back to sequential factorization in environments without worker thread support

Parameters:
NameTypeAttributesDescription
nnumber | string | BigInt

The number to factorize

optionsObject<optional>

Factorization options

Properties
NameTypeAttributesDefaultDescription
workerCountnumber<optional>

Number of worker threads to use (default: CPU count - 1)

useWorkStealingboolean<optional>
true

Whether to use work stealing for load balancing

Throws:

If n is not a positive integer

Type
PrimeMathError
Returns:

A map where keys are prime factors and values are their exponents

Type: 
Map.<BigInt, BigInt>

(inner) factorizePollardsRho(n, optionsopt) → {Map.<BigInt, BigInt>}

Factorize a large number using enhanced Pollard's Rho algorithm Improved with Prime Framework optimizations for performance

Parameters:
NameTypeAttributesDescription
nnumber | string | BigInt

The number to factorize

optionsObject<optional>

Factorization options

Properties
NameTypeAttributesDefaultDescription
useCacheboolean<optional>
true

Whether to use factorization cache

perfectFactorizationboolean<optional>
true

Whether to ensure complete factorization

Throws:

If n is not a positive integer

Type
PrimeMathError
Returns:

A map where keys are prime factors and values are their exponents

Type: 
Map.<BigInt, BigInt>

(inner) factorizeWithPrimes(n) → {Map.<BigInt, BigInt>}

Factorize a number using optimized trial division with precomputed primes This is more efficient for moderately sized numbers

Parameters:
NameTypeDescription
nnumber | string | BigInt

The number to factorize

Throws:

If n is not a positive integer

Type
PrimeMathError
Returns:

A map where keys are prime factors and values are their exponents

Type: 
Map.<BigInt, BigInt>

(inner) findFactorsPollardRho(n, factors, optionsopt) → {Map.<BigInt, BigInt>}

Recursively find factors using enhanced factorization algorithms Optimized with Prime Framework techniques for finding factors Implements a sophisticated approach that selects the most efficient algorithm for each phase of the factorization, following the Prime Framework requirements

Parameters:
NameTypeAttributesDescription
nBigInt

The number to factorize

factorsMap.<BigInt, BigInt>

The current factor map

optionsObject<optional>

Algorithm options

Returns:

Updated factor map

Type: 
Map.<BigInt, BigInt>

(inner) fromPrimeFactors(factors, optionsopt) → {BigInt}

Create a number from its prime factorization Implements the Prime Framework's universal coordinate system conversion

Parameters:
NameTypeAttributesDescription
factorsArray.<PrimeFactor> | Map.<BigInt, BigInt>

Array of {prime, exponent} objects or Map of prime->exponent

optionsObject<optional>

Conversion options

Properties
NameTypeAttributesDefaultDescription
validatePrimalityboolean<optional>
true

Whether to validate that each factor is prime

enforceCanonicalFormboolean<optional>
true

Whether to enforce canonical form (e.g., sort factors)

Throws:

If any of the factors is not a prime number or has a non-positive exponent

Type
PrimeMathError
Returns:

The number represented by the given prime factorization

Type: 
BigInt

(inner) getErrorMessage(error) → {string}

Helper function to safely extract error message Non-recursive implementation to avoid stack overflows

Parameters:
NameTypeDescription
errorunknown

Any error value

Returns:

The error message

Type: 
string

(inner) getPrimeFactors(n) → {Array.<BigInt>}

Get unique prime factors of a number (without exponents)

Parameters:
NameTypeDescription
nnumber | string | BigInt

The number to get prime factors for

Returns:

Array of prime factors (without repetition)

Type: 
Array.<BigInt>

(inner) getPrimeSignature(n) → {BigInt}

Find the prime signature of a number (product of (p_i - 1)(p_i^e_i - 1)) Used in various number theory contexts

Parameters:
NameTypeDescription
nnumber | string | BigInt

The number to find the signature for

Returns:

The prime signature

Type: 
BigInt

(inner) getRadical(n) → {BigInt}

Find the radical of a number (product of distinct prime factors)

Parameters:
NameTypeDescription
nnumber | string | BigInt

The number to find the radical for

Returns:

The radical of n

Type: 
BigInt

(inner) isFactorizationComplete(factors, original) → {boolean}

Check if the factorization of a number is complete

Parameters:
NameTypeDescription
factorsMap.<BigInt, BigInt>

The factorization to check

originalBigInt

The original number

Returns:

True if the factorization is complete, false otherwise

Type: 
boolean

(inner) millerRabinTest(n, k) → {boolean}

Miller-Rabin primality test for larger numbers

Parameters:
NameTypeDefaultDescription
nBigInt

The number to test for primality

knumber25

Number of iterations for accuracy (higher is more accurate)

Returns:

True if n is probably prime, false if definitely composite

Type: 
boolean

(inner) modularFastExp(base, exponent, modulus) → {BigInt}

Fast modular exponentiation for Miller-Rabin test

Parameters:
NameTypeDescription
baseBigInt

Base value

exponentBigInt

Exponent value

modulusBigInt

Modulus for the operation

Returns:

Result of (base^exponent) % modulus

Type: 
BigInt

(inner) pollardRho(n, optionsopt) → {BigInt}

Enhanced Pollard's Rho algorithm with cycle detection Optimized for the Prime Framework's factorization requirements

Parameters:
NameTypeAttributesDescription
nBigInt

The number to factor

optionsObject<optional>

Algorithm options

Properties
NameTypeAttributesDefaultDescription
maxIterationsnumber<optional>

Maximum number of iterations (if not specified, no limit is applied)

cBigInt<optional>
1n

Polynomial constant

timeLimitnumber<optional>

Maximum time to spend in milliseconds (if not specified, no time limit is applied)

Returns:

A non-trivial factor of n, or n if no factor is found

Type: 
BigInt

(inner) quadraticSieve(n, optionsopt) → {BigInt}

Quadratic Sieve implementation for factoring large numbers Implements a complete and efficient factorization algorithm aligned with the Prime Framework

Parameters:
NameTypeAttributesDescription
nBigInt

The number to factor

optionsObject<optional>

Algorithm options

Properties
NameTypeAttributesDefaultDescription
factorBaseSizenumber<optional>
100

Size of the factor base

sieveSizenumber<optional>
10000

Size of the sieve interval

numRelationsnumber<optional>
0

Number of relations to collect (0 = auto)

verboseboolean<optional>
false

Whether to output debug info

Throws:

If input is not a positive composite number

Type
PrimeMathError
Returns:

A non-trivial factor of n, or n if no factor is found

Type: 
BigInt

Type Definitions

FactorizationResult

Type:
  • Object
Properties
NameTypeAttributesDescription
factorsMap.<BigInt, BigInt>

Map of prime factors where key is the prime and value is the exponent

isCompleteboolean

Indicates if the factorization is complete (true) or partial (false)

confidencenumber<optional>

Confidence level for primality testing (0-1)

PrimeFactor

Type:
  • Object
Properties
NameTypeDescription
primeBigInt

The prime number

exponentBigInt

The exponent (power) of the prime

WorkerConfig

Type:
  • Object
Properties
NameTypeAttributesDescription
threadCountnumber<optional>

Number of worker threads to use

enableWorkStealingboolean<optional>

Whether to enable work stealing

chunkSizenumber<optional>

Size of work chunks for distribution