perm_fl()
Calculate P(n, k)
The function perm_fl() is a user-defined function (UDF) that calculates P(n, k), the number of permutations for selection of k items out of n, with order. It’s based on the native gamma() function to calculate factorial, (see facorial_fl()). For selection of k items without order, use comb_fl().
Syntax
perm_fl(n, k)
Parameters
| Name | Type | Required | Description |
|---|
Function definition
You can define the function by either embedding its code as a query-defined function, or creating it as a stored function in your database, as follows:
Query-defined
Define the function using the following let statement. No permissions are required.
let perm_fl=(n:int, k:int)
{
let fact_n = gamma(n+1);
let fact_nk = gamma(n-k+1);
tolong(fact_n/fact_nk)
};
// Write your query to use the function here.
Stored
Define the stored function once using the following .create function. Database User permissions are required.
.create-or-alter function with (folder = "Packages\\Stats", docstring = "Calculate number of permutations for selection of k items out of n items with order")
perm_fl(n:int, k:int)
{
let fact_n = gamma(n+1);
let fact_nk = gamma(n-k+1);
tolong(fact_n/fact_nk)
}
Example
Query-defined
To use a query-defined function, invoke it after the embedded function definition.
let perm_fl=(n:int, k:int)
{
let fact_n = gamma(n+1);
let fact_nk = gamma(n-k+1);
tolong(fact_n/fact_nk)
}
;
range n from 3 to 10 step 3
| extend k = n-2
| extend pnk = perm_fl(n, k)
Stored
range n from 3 to 10 step 3
| extend k = n-2
| extend pnk = perm_fl(n, k)
Output
| n | k | pnk |
|---|---|---|
| 3 | 1 | 3 |
| 6 | 4 | 360 |
| 9 | 7 | 181440 |
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.