Example #1
0
        /// <summary>
        /// Calculates the least common multiple of a collection of numbers
        /// </summary>
        /// <param name="numbers">The numbers to find the LCM of</param>
        /// <returns>The factorization of the LCM of <paramref name="numbers"/></returns>
        public static Factorization LCM(params long[] numbers)
        {
            if (numbers == null)
            {
                throw new ArgumentNullException(nameof(numbers));
            }

            if (numbers.Length < 1)
            {
                throw new ArgumentException("At least one number is required for finding the least common multiple!", nameof(numbers));
            }

            var factorizations = numbers.Select(n => Factorization.Of(n)).ToList();
            var allPrimes      = new HashSet <long>(factorizations.SelectMany(d => d.Keys));

            return(new Factorization(allPrimes.Select(p => (p, factorizations.Max(f => f.GetExponent(p))))));
        }
Example #2
0
 /// <summary>
 /// Calculates a binomial coefficient C(n, k)
 /// </summary>
 /// <param name="n">The number of items to select from</param>
 /// <param name="k">The number of items to select</param>
 /// <returns>A binomial coefficient C(n, k), ie. the number of ways to choose <paramref name="k"/> items from <paramref name="n"/> distinct ones</returns>
 public static Factorization Binomial(int n, int k)
 {
     return(Factorization.OfFactorial(n) / (Factorization.OfFactorial(k) * Factorization.OfFactorial(n - k)));
 }