/// <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) )); }
/// <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) )); }