SQL Server PERMUTATIONA Function
Updated 2024-03-13 13:09:18.090000
Description
Use the scalar function PERMUTATIONA to calculate the number of permutations with replacement defined as:
P_{k,n}=n^k
Syntax
SELECT [westclintech].[wct].[PERMUTATIONA] (
<@n, int,>
,<@k, int,>)
Arguments
@n
Number of items.
@k
Number chosen.
Return Type
float
Remarks
If n < 0 and k < 0 then NULL is returned.
If k < 0 then NULL is returned.
Examples
Example #1
SELECT wct.PERMUTATIONA(10, 3) as PERMUTATIONA;
This produces the following result.
| PERMUTATIONA |
|---|
| 1000 |
Example #2
Calculate the probability of 2 people having the same birthday in a room containing n people.
SELECT x.n,
1 - wct.PERMUT(365, x.n) / wct.PERMUTATIONA(365, x.n) as [p(n)]
FROM
(
VALUES
(10),
(20),
(22),
(23),
(30),
(57)
) x (n);
This produces the following result.
| n | p(n) |
|---|---|
| 10 | 0.116948177711078 |
| 20 | 0.41143838358058 |
| 22 | 0.47569530766255 |
| 23 | 0.507297234323985 |
| 30 | 0.706316242719269 |
| 57 | 0.99012245934117 |
See Also
FACTORIALPOWER - the step-h factorial power x(n,h)
LCHOOSE - Natural logarithm of the binomial coefficient
LPERMUT - Calculate the natural logarithm of the PERMUT function.
LPERMUTA - calculate the natural logarithm of the PERMUTATIONA function.