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
        /// <summary>
        /// Calculates the total number of permutations that will be returned.
        /// As this can grow very large, extra effort is taken to avoid overflowing the accumulator.
        /// While the algorithm looks complex, it really is just collecting numerator and denominator terms
        /// and cancelling out all of the denominator terms before taking the product of the numerator terms.
        /// </summary>
        /// <returns>The number of permutations.</returns>
        private BigInteger GetCount()
        {
            var runCount   = 1;
            var divisors   = Enumerable.Empty <int>();
            var numerators = Enumerable.Empty <int>();

            for (var i = 1; i < _myLexicographicOrders.Length; ++i)
            {
                numerators = numerators.Concat(SmallPrimeUtility.Factor(i + 1));

                if (_myLexicographicOrders[i] == _myLexicographicOrders[i - 1])
                {
                    ++runCount;
                }
                else
                {
                    for (var f = 2; f <= runCount; ++f)
                    {
                        divisors = divisors.Concat(SmallPrimeUtility.Factor(f));
                    }

                    runCount = 1;
                }
            }

            for (var f = 2; f <= runCount; ++f)
            {
                divisors = divisors.Concat(SmallPrimeUtility.Factor(f));
            }

            return(SmallPrimeUtility.EvaluatePrimeFactors(
                       SmallPrimeUtility.DividePrimeFactors(numerators, divisors)
                       ));
        }
Example #2
0
        /// <summary>
        /// Calculates the total number of permutations that will be returned.
        /// As this can grow very large, extra effort is taken to avoid overflowing the accumulator.
        /// While the algorithm looks complex, it really is just collecting numerator and denominator terms
        /// and cancelling out all of the denominator terms before taking the product of the numerator terms.
        /// </summary>
        /// <returns>The number of permutations.</returns>
        private long GetCount()
        {
            var runCount   = 1;
            var divisors   = new List <int>();
            var numerators = new List <int>();

            for (var i = 1; i < _myLexicographicOrders.Length; ++i)
            {
                numerators.AddRange(SmallPrimeUtility.Factor(i + 1));

                if (_myLexicographicOrders[i] == _myLexicographicOrders[i - 1])
                {
                    ++runCount;
                }
                else
                {
                    for (var f = 2; f <= runCount; ++f)
                    {
                        divisors.AddRange(SmallPrimeUtility.Factor(f));
                    }
                    runCount = 1;
                }
            }

            for (var f = 2; f <= runCount; ++f)
            {
                divisors.AddRange(SmallPrimeUtility.Factor(f));
            }

            return(SmallPrimeUtility.EvaluatePrimeFactors(
                       SmallPrimeUtility.DividePrimeFactors(numerators, divisors)
                       ));
        }