static public long Choose(int n, int k, bool isSorted, bool isUnique) { long result = 1; if (!isSorted && !isUnique) { for (int i = 0; i < k; i++) { result *= n; } } if (!isSorted && isUnique) { result = Combinatorial.C(n, k) * Combinatorial.Factorial(k); } if (isSorted && isUnique) { result = Combinatorial.C(n, k); } if (isSorted && !isUnique) { result = Combinatorial.C(n + k - 1, k); } return(result); }
public long count(string n) { long result = 0; int[] digits = new int[10]; for (int i = 0; i < n.Length; i++) { digits[n[i] - '0']++; } while (n != "") { for (int i = 0; i < n[0] - '0'; i++) { if (digits[i] > 0) { int k = n.Length - 1; long x = 1; digits[i]--; for (int j = 0; j < 10; j++) { x *= Combinatorial.C(k, digits[j]); k -= digits[j]; } digits[i]++; result += x; } } digits[n[0] - '0']--; n = n.Substring(1); } return(result); }
/// <summary> /// Register as /value1/1/value2/2 /// </summary> public static void RegisterUrlNamedParameters(this RouteCollection routes, string urlPrefix, string controller, string action, RoutingParameter[] parameters) { foreach (var parameterCombination in Combinatorial.GetAllSubset(parameters.Length)) { var url = urlPrefix; var routeValues = new RouteValueDictionary { { "controller", controller }, { "action", action } }; foreach (var i in parameterCombination) { var parameter = parameters[i]; url += string.Format("/{0}/{{{1}}}", parameter.NameInUrl, parameter.NameInAction); } foreach (var parameter in parameters) { routeValues.Add(parameter.NameInAction, parameter.DefaultValue); } routes.Add(Guid.NewGuid().ToString(), new Route(url, routeValues, new MvcRouteHandler())); } }
public void ABxB() { var ab_b = AB * B; var expectedTypes = new[] { A.ItemType(), B.ItemType(), B.ItemType() }; Assert.That(ab_b, Is.EqualTo(Combinatorial.Of(A, B, B))); Assert.That(ab_b.GetType().GetGenericArguments(), Is.EquivalentTo(expectedTypes)); }
void TestPermutations(int n, string[] expected) { List <string> valueList = new List <string>(16); Combinatorial.Permutations(n, x => { string s = string.Join(string.Empty, x); valueList.Add(s); }); CollectionAssert.AreEqual(expected, valueList); }
public void ABxDecimal() { var decimals = new List <decimal> { 1, 2, 3 }; var ab_dec = AB.Multiply(decimals); var expectedTypes = new[] { A.ItemType(), B.ItemType(), decimals.ItemType() }; Assert.That(ab_dec, Is.EqualTo(Combinatorial.Of(A, B, decimals))); Assert.That(ab_dec.GetType().GetGenericArguments(), Is.EquivalentTo(expectedTypes)); }
protected override void Combinations(IList <int> list, int k, Action <int[]> action) { Combinatorial.Combinations(list, k, action); }
public void PermutationsGuardCase3Test() { Combinatorial.Permutations(1, null); }
public void PermutationsGuardCase2Test() { Combinatorial.Permutations(-1, x => { }); }