C# Class Crisis.SmallPrimeUtility

Utility class that maintains a small table of prime numbers and provides simple implementations of Prime Factorization algorithms. This is a quick and dirty utility class to support calculations of permutation sets with indexes under 2^31. The prime table contains all primes up to Sqrt(2^31) which are all of the primes requires to factorize any Int32 positive integer.
Exibir arquivo Open project: teeknofil/Crisis-Wordlist-Generator

Public Methods

Method Description
DividePrimeFactors ( IList numerator, IList denominator ) : List

Given two integers expressed as a list of prime factors, divides these numbers and returns an integer also expressed as a set of prime factors. If the result is not a integer, then the result is undefined. That is, 11 / 5 when divided by this function will not yield a correct result. As such, this function is ONLY useful for division with combinatorial results where the result is known to be an integer AND the division occurs as the last operation(s).

EvaluatePrimeFactors ( IList value ) : long

Given a list of prime factors returns the long representation.

Factor ( int i ) : List

Performs a prime factorization of a given integer using the table of primes in PrimeTable. Since this will only factor Int32 sized integers, a simple list of factors is returned instead of the more scalable, but more difficult to consume, list of primes and associated exponents.

MultiplyPrimeFactors ( IList lhs, IList rhs ) : List

Given two integers expressed as a list of prime factors, multiplies these numbers together and returns an integer also expressed as a set of prime factors. This allows multiplication to overflow well beyond a Int64 if necessary.

Private Methods

Method Description
CalculatePrimes ( ) : void

Calculate all primes up to Sqrt(2^32) = 2^16. This table will be large enough for all factorizations for Int32's. Small tables are best built using the Sieve Of Eratosthenes, Reference: http://primes.utm.edu/glossary/page.php?sort=SieveOfEratosthenes

SmallPrimeUtility ( ) : System.Collections.Generic

Utility class, no instances allowed.

Method Details

DividePrimeFactors() public static method

Given two integers expressed as a list of prime factors, divides these numbers and returns an integer also expressed as a set of prime factors. If the result is not a integer, then the result is undefined. That is, 11 / 5 when divided by this function will not yield a correct result. As such, this function is ONLY useful for division with combinatorial results where the result is known to be an integer AND the division occurs as the last operation(s).
public static DividePrimeFactors ( IList numerator, IList denominator ) : List
numerator IList Numerator argument, expressed as list of prime factors.
denominator IList Denominator argument, expressed as list of prime factors.
return List

EvaluatePrimeFactors() public static method

Given a list of prime factors returns the long representation.
public static EvaluatePrimeFactors ( IList value ) : long
value IList Integer, expressed as list of prime factors.
return long

Factor() public static method

Performs a prime factorization of a given integer using the table of primes in PrimeTable. Since this will only factor Int32 sized integers, a simple list of factors is returned instead of the more scalable, but more difficult to consume, list of primes and associated exponents.
public static Factor ( int i ) : List
i int The number to factorize, must be positive.
return List

MultiplyPrimeFactors() public static method

Given two integers expressed as a list of prime factors, multiplies these numbers together and returns an integer also expressed as a set of prime factors. This allows multiplication to overflow well beyond a Int64 if necessary.
public static MultiplyPrimeFactors ( IList lhs, IList rhs ) : List
lhs IList Left Hand Side argument, expressed as list of prime factors.
rhs IList Right Hand Side argument, expressed as list of prime factors.
return List