comb_fl()
Calculate C(n, k)
The function comb_fl() is a user-defined function (UDF) that calculates C(n, k), the number of combinations for selection of k items out of n, without order. It’s based on the native gamma() function to calculate factorial. For more information, see facorial_fl(). For a selection of k items with order, use perm_fl().
Syntax
comb_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 comb_fl=(n:int, k:int)
{
let fact_n = gamma(n+1);
let fact_nk = gamma(n-k+1);
let fact_k = gamma(k+1);
tolong(fact_n/fact_nk/fact_k)
};
// 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 combinations for selection of k items out of n items without order")
comb_fl(n:int, k:int)
{
let fact_n = gamma(n+1);
let fact_nk = gamma(n-k+1);
let fact_k = gamma(k+1);
tolong(fact_n/fact_nk/fact_k)
}
Example
Query-defined
To use a query-defined function, invoke it after the embedded function definition.
let comb_fl=(n:int, k:int)
{
let fact_n = gamma(n+1);
let fact_nk = gamma(n-k+1);
let fact_k = gamma(k+1);
tolong(fact_n/fact_nk/fact_k)
};
range n from 3 to 10 step 3
| extend k = n-2
| extend cnk = comb_fl(n, k)
Stored
range n from 3 to 10 step 3
| extend k = n-2
| extend cnk = comb_fl(n, k)
Output
| n | k | cnk |
|---|---|---|
| 3 | 1 | 3 |
| 6 | 4 | 15 |
| 9 | 7 | 36 |
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.