static long distinctCount(IEnumerable <long> source, long length, long remaining, long skippable) { if (skippable < 0L) { return(0L); } if (skippable == 0L) { return(Combinatoric.Multinomial(source)); } var count = source.First(); if (count >= length) { return(remaining.Power((int)length)); } var others = source.Skip(1); var result = distinctCount(others, length, remaining - 1L, skippable - count); for (var x = 1L; x <= count; x++) { var temp = distinctCount(others, length - x, remaining - 1L, skippable + x - count); result = checked (result + Combinatoric.Choose(length, x) * temp); } return(result); }
public static void Multinomial() { //this example shows how to get multinomial //use Combinatoric.Multinomial(collection_of_x) to get multinomial Console.WriteLine(Combinatoric.Multinomial(new[] { 1, 4, 4, 2 })); }
public static void Choose() { //this example shows how to get binomial //use Combinatoric.Choose(n, k) for n!/(k!*(n-k)!) Console.WriteLine(Combinatoric.Choose(10, 5)); }
public static void FallingFactorial() { //this example shows how to get falling factorial //use Combinatoric.FallingFactorial(n, r) for n!/r! Console.WriteLine(Combinatoric.FallingFactorial(10, 5)); }
public static void Factorial() { //this example shows how to get factorial //use Combinatoric.Factorial(n) for n! Console.WriteLine(Combinatoric.Factorial(10)); }
//constructors //static methods static long count(IEnumerable <T> source, int length, CombinatoricModel model) { switch (model) { case CombinatoricModel.Normal: return(Combinatoric.FallingFactorial((long)source.Count(), length)); case CombinatoricModel.Repetition: return(((long)source.Count()).Power(length)); case CombinatoricModel.Distinct: var lookup = source.ToLookup(x => x); return(distinctCount(lookup.Select(x => x.LongCount()).OrderBy(x => x).ToList(), length, lookup.Count, source.Count() - length)); case CombinatoricModel.RepetitionDistinct: return(source.Distinct().LongCount().Power(length)); default: return(0L); } }
static long count(IEnumerable <T> source) { var length = source.Count() - 1L; return(Combinatoric.Choose(2L * length, length) / (length + 1L)); }