Combinatronic functions

ARRANGEMENTS

Reference

» Reference, Discussion, & Example Applications:

About

Calls AFLAT.

Inputs:

  • a : array
  • t : type argument,”pa”, permutations with repetitions; “p” permutations w/o repetititions; “ca”, combinations with repetitions; “c”, combinations w/o repetitions
  • c : number_chosen

More Info:

  1. PERMUTATIONA: Returns the number of permutations for a given number of objects (with repetitions) that can be selected from the total objects.
    • PERMUTATIONA(number, number-chosen); “c” (number chosen, nc) can be >= n (number of elements/objects); order is important; PA=n^nc
  2. PERMUT: Returns the number of permutations for a given number of objects (no repetitions) that can be selected from the total objects.
    • PERMUT(number, number_chosen); if nc > n returns #NUM! error; also called arrangements; order is important; P=n!/(n-nc)!
  3. COMBINA: Returns the number of combinations (with repetitions) for a given number of items.
    • COMBINA(number, number_chosen); nc can be > n; order is not important; CA=(n+nc-1)!/(nc!*(n-1)!)
  4. COMBIN: Returns the number of combinations (no repetitions) for a given number of items.
    • COMBIN(number, number_chosen); if nc > n returns #NUM! error; order is not important; C=P/nc! or C=n!/(nc!*(n-nc)!)

Code

M.S. Excel
// Helper tool Lambdas
T_PA = LAMBDA(n, c, MOD(ROUNDUP(SEQUENCE(n ^ c) / n ^ (c - SEQUENCE(, c)), 0) - 1, n) + 1);
T_P = LAMBDA(a, [ai], [i],
    LET(
        n, COLUMNS(a),
        j, IF(i = "", n, i),
        x, INDEX(a, , j),
        IF(j = 0, FILTER(a, MMULT(ai, SEQUENCE(n) ^ 0) = n), T_P(a, ai + (x = a), j - 1))
    )
);
T_CA LAMBDA(a, [ai], [i],
    LET(
        n, COLUMNS(a),
        j, IF(i = "", 1, i),
        aj, IF(ai = "", 1, ai),
        x, INDEX(a, , j),
        IF(j = n, FILTER(a, aj), T_CA(a, aj * (x <= INDEX(a, , j + 1)), j + 1))
    )
);

ARRANGEMENTS = LAMBDA(a, t, c,
    IF(
        AND(t <> {"p", "pa", "c", "ca"}),
        "check type",
        LET(
            k, MAX(1, c),
            x, AFLAT(a),
            n, ROWS(x),
            IF(
                AND(OR(t = {"p", "c"}), k > n),
                "nr chosen > n !!!",
                LET(
                    y, T_PA(n, k),
                    SWITCH(
                        t,
                        "pa",
                        INDEX(x, y),
                        "p",
                        INDEX(x, T_P(y)),
                        "ca",
                        INDEX(x, T_CA(y)),
                        "c",
                        LET(z, T_P(y), w, T_CA(z), INDEX(x, w))
                    )
                )
            )
        )
    )
);