Skip to content

KPermutations ​

The kk-permutations of {1,…,n}\{1, \dots, n\}: ordered selections of kk distinct elements, same family as Arrangements.

✓ checked against that system · ! checked, with disagreements (hover a chip)OEISA008279✓
KPermutations(n, k)the n!/(n−k)!n!/(n-k)! ordered kk-element selections from {1,…,n}\{1, \dots, n\}
Typed signatures
TypePackageApplies
(integer<0..>, integer<0..>) -> indexed_collection<list<integer>>combinatoricsalways

Domain: Collections

Definition

Epsil, as the implementation runs it.

count(_n, _k)

Fold(Function(product * f, product, f), 1, Range(_n - _k + 1, _n, 1)) if _k <=
                                                                         _n else 0

unrank(_n, _k, _r)

Map(Function(Fold(
                   Function(v + 1 if v >= (_r - _r % Fold(
                                                           Function(
                                                                     product * f,
                                                                     product,
                                                                     f,
                                                                   ),
                                                           1,
                                                           Range(
                                                                  _n - i - (_k -
                                                                            i) +
                                                                  1,
                                                                  _n - i,
                                                                  1,
                                                                ),
                                                         )) / Fold(
                                                                   Function(
                                                                             product *
                                                                             f,
                                                                             product,
                                                                             f,
                                                                           ),
                                                                   1,
                                                                   Range(
                                                                          _n - i -
                                                                          (_k -
                                                                           i) + 1,
                                                                          _n - i,
                                                                          1,
                                                                        ),
                                                                 ) % (_n - i + 1) +
                                          1 else v, v, i),
                   (_r - _r % Fold(
                                    Function(product * f, product, f),
                                    1,
                                    Range(_n - j - (_k - j) + 1, _n - j, 1),
                                  )) / Fold(
                                            Function(product * f, product, f),
                                            1,
                                            Range(
                                                   _n - j - (_k - j) + 1,
                                                   _n - j,
                                                   1,
                                                 ),
                                          ) % (_n - j + 1) + 1,
                   Range(j - 1, 1, -1),
                 ), j), Range(1, _k, 1))

rank(_n, _k, _x)

Fold(
      Function(
                r * (_n - j + 1) + (_x[j] - 1 - Fold(
                                                      Function(c +
                                                               (1 if _x[i] <
                                                                     _x[j] else 0), c, i),
                                                      0,
                                                      Range(1, j - 1, 1),
                                                    )),
                r,
                j,
              ),
      0,
      Range(1, _k, 1),
    )

valid(_n, _k, _x)

Fold(Function(
               (ReplaceAt(di_seen, _x[di_j], 1) if di_seen[_x[di_j]] == 0 else ReplaceAt(
                                                                                          di_seen,
                                                                                          _n + 1,
                                                                                          1,
                                                                                        )) if 1 <= _x[di_j] &&
                                                                                             _x[di_j] <= _n else ReplaceAt(di_seen, _n + 1, 1),
               di_seen,
               di_j,
             ), Map(Function(0, di_z), Range(1, _n + 1, 1)), Range(1, _k, 1))[_n +
                                                                             1] ==
0 if Length(_x) == _k else False

Examples

Basic #

5!/3!=205!/3! = 20

live ↗
InCount(KPermutations(5,2))\mathrm{Count}(\mathrm{KPermutations}(5, 2))Out2020

Ordered pairs of distinct elements of {1,2,3}\{1,2,3\}

live ↗
InTake(KPermutations(3,2),6)\mathrm{Take}(\mathrm{KPermutations}(3, 2), 6)Out[[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]]\bigl\lbrack\bigl\lbrack1, 2\bigr\rbrack, \bigl\lbrack1, 3\bigr\rbrack, \bigl\lbrack2, 1\bigr\rbrack, \bigl\lbrack2, 3\bigr\rbrack, \bigl\lbrack3, 1\bigr\rbrack, \bigl\lbrack3, 2\bigr\rbrack\bigr\rbrack
About
Examples2
CrosswalkOEIS

See also: Arrangements