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.
Пример #1
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 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)
                       ));
        }
Пример #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)
                       ));
        }