# permutation

Returns the number of unique subsets created from all permutations of a number of elements. The order of the chosen elements is significant.

$$\mathrm{P}_{k}^{n} = \frac{n!}{(n-k)!}$$

If Alice, Bob, and Carol are playing a board game, the order they take turns can be one of $$P(3,3) = 6$$ possibilities. They are: ABC, ACB, BAC, BCA, CAB, CBA.

In a race of six Olympians, only three can win bronze, silver, and gold medals. The number of possible medalist outcomes is equal to $$P(6,3) = 120$$.

permutation(set, subset)
Returns the number of unique subsets created from all permutations of a number of elements.
COPY/// @func   permutation(set, subset)
///
/// @desc   Returns the number of unique subsets created from all
///         permutations of a number of elements. The order of the
///         chosen elements is significant. Returns (-1) on error.
///
/// @param  {real}      set         number of elements
/// @param  {real}      subset      size of the subset
///
/// @return {real}      number of permutations
///

function permutation(set, subset)
{
var n = floor(set);
var k = floor(subset);
var m = n - k;
if (m < 0) return (-1);
var f = 1;
for (var l=n; l>m; l-=1) f *= l;
return f;
}

Contributors: xot

GitHub: View · Commits · Blame · Raw