SQL Server LPERMUT Function
Updated 2024-03-08 21:45:15.067000
Description
Use the scalar function LPERMUT to calculate the natural logarithm of the PERMUT function. The PERMUT function calculates the number of permutations without replacement defined as:
_nP_k=\frac{n!}{\left(n-k\right)!
Syntax
SELECT [westclintech].[wct].[LPERMUT] (
<@n, int,>
,<@k, int,>)
Arguments
@n
Number of items.
@k
Number chosen.
Return Type
float
Remarks
If n < 0 then NULL is returned.
If k < 0 then NULL is returned.
If k > n then NULL is returned.
Examples
Example #1
SELECT wct.LPERMUT(10, 3) as LPERMUT;
This produces the following result.
| LPERMUT |
|---|
| 6.5792512120101 |
Example #2
Calculate the probability of 2 people having the same birthday in a room containing n people.
SELECT x.n,
1 - EXP(wct.LPERMUT(365, x.n) - wct.LPERMUTA(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.116948177711411 |
| 20 | 0.411438383580767 |
| 22 | 0.475695307662689 |
| 23 | 0.507297234324094 |
| 30 | 0.706316242719354 |
| 57 | 0.990122459341174 |
See Also
FACTORIALPOWER - the step-h factorial power x(n,h)
LCHOOSE - Natural logarithm of the binomial coefficient
LPERMUTA - calculate the natural logarithm of the PERMUTATIONA function.