Permutations and Combinations - Gnorion/BizVR GitHub Wiki

Permutations and Combinations

{x,y,z in [1..4] where distinct, x<y, y<z} Generates these combinations 123, 124, 234, 134

{x,y,z in [1..3] where distinct} Generates permutations of 3 out of 3 123,132,213,231,312,321

And without the ‘distinct’ you’d get all 27 permutations: 111, 112, 113,...333

We can also define functions to compute the values of nCr and nPr as follows:

FUNCTION:combinations(n,r)->number

image

FUNCTION:permutations(n,r)->number

image

The total number of permutations allowing for repetition would simply be n^r

Robustness

Since these functions are only defined for certain values of n and r we can make the function more robust as follows: image

We could also add meaningful text messages to describe the errors more precisely

FUNCTION: factorial(n)

The factorial function might be defined like this (recursively) image