Factorial and gamma functions
Use these functions and the ! (factorial) operator to compute factorials, binomial coefficients, and the gamma function, which extends the factorial to numbers that are not integers. For large inputs, use lgamma, which stays finite where gamma returns infinity.
! (factorial) operator
Returns the product of all whole numbers from 1 to x, so 5! returns 120, which is 1 times 2 times 3 times 4 times 5. The factorial of 0 is 1. The result is a HUGEINT, which can hold factorials up to 33!. If x is NULL, the result is NULL.
Write ! after the value. factorial is an alias of !: factorial( returns the same result as x).x!
Syntax
x!
factorial(x)
Arguments
The ! (factorial) operator takes the following argument:
x-
The number to compute the factorial of, as an
INTEGERvalue from0to33. ATINYINT,SMALLINT,UTINYINT, orUSMALLINTvalue is converted toINTEGER. Cast a value of any other numeric type, such asBIGINT,UINTEGER,DECIMAL, orDOUBLE, toINTEGERfirst.
Return type
HUGEINT
Examples
The following SQL query returns the factorial of 5.
SELECT 5! AS result
The query returns the following result.
| result |
|---|
|
The following SQL query uses the factorial alias, shows that the factorial of 0 is 1, and shows that a NULL input returns NULL.
SELECT factorial(4) AS factorial_function, 0! AS zero, factorial(NULL) AS null_input
The query returns the following result.
| factorial_function | zero | null_input |
|---|---|---|
|
|
|
The following SQL query returns 21!, which is too large for a BIGINT but fits in the HUGEINT result.
SELECT 21! AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that ! is applied before a minus sign and before multiplication.
SELECT -3! AS sign_after, 2 * 3! AS product
The query returns the following result.
| sign_after | product |
|---|---|
|
|
Considerations
The query fails if x is negative, or if it is greater than 33, because 34! does not fit in a HUGEINT.
A BIGINT argument, such as a BIGINT column or the result of count(*), fails. Cast it first, as in factorial(CAST(.n AS INTEGER))
! is applied before a minus sign and before the other arithmetic operators, so -3! returns -6, the negative of 3!, and 2 * 3! returns 12.
binom function
Returns the number of ways to choose k items from a set of n items when the order of the items does not matter. If k is greater than n, the result is 0. If either argument is NULL, the result is NULL.
Syntax
binom(n, k)
Arguments
The binom function takes the following arguments:
n-
The number of items in the set, as an
INTEGER. k-
The number of items to choose, as an
INTEGER.
Return type
HUGEINT
Examples
The following SQL query returns the number of ways to choose 2 items from 5.
SELECT binom(5, 2) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns the number of different 5-card hands that you can deal from a 52-card deck.
SELECT binom(52, 5) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query asks for more items than the set has, so the result is 0.
SELECT binom(5, 6) AS result
The query returns the following result.
| result |
|---|
|
Considerations
Both arguments must be INTEGER values or smaller integer types. A BIGINT argument is not accepted, so cast a BIGINT column to INTEGER first.
If n or k is negative, the query fails.
If the result is too large for HUGEINT, the query fails. For example, binom(130, 65) returns a result, but binom(131, 65) fails.
gamma function
Returns the gamma function of x. For a positive integer n, gamma( equals the factorial of n), so n - 1gamma(5) returns 24.0, the factorial of 4. Unlike the factorial, x does not have to be an integer. If x is NULL, the result is NULL.
To compute the factorial of an integer, use factorial. For large inputs, use the lgamma function, which returns the natural logarithm of the gamma function and stays finite.
Syntax
gamma(x)
Arguments
The gamma function takes the following argument:
x-
The input to the gamma function, as a
DOUBLEvalue. Values of other numeric types are converted toDOUBLE.
Return type
DOUBLE
Examples
The following SQL query returns the gamma function of 5.5.
SELECT gamma(5.5) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query returns the gamma function of 5, which is the factorial of 4.
SELECT gamma(5) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that the gamma function of 0 is infinity, and that the gamma function of a negative integer is NaN.
SELECT isinf(gamma(0)) AS at_zero, isnan(gamma(-1)) AS at_negative_integer
The query returns the following result.
| at_zero | at_negative_integer |
|---|---|
|
|
Considerations
gamma(0) returns infinity, and a negative integer returns NaN (not a number). The query does not fail in either case. To find such results, test them with isinf and isnan.
For x greater than about 171.6, the result is too large for DOUBLE, and the function returns infinity. For such values, use the lgamma function.
lgamma function
Returns the natural logarithm of the absolute value of gamma(. The logarithm grows much more slowly than the gamma function, so x)lgamma returns a finite result for large inputs where gamma returns infinity, such as 200. If x is NULL, the result is NULL.
For a positive integer n, exp(lgamma( is approximately the factorial of n)).n - 1
Syntax
lgamma(x)
Arguments
The lgamma function takes the following argument:
x-
The input to the gamma function, as a
DOUBLEvalue. Values of other numeric types are converted toDOUBLE.
Return type
DOUBLE
Examples
The following SQL query returns the logarithm of the gamma function of 2. gamma(2) is 1, so the result is 0.
SELECT lgamma(2) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query recovers the factorial of 9 from lgamma(10) and rounds the result.
SELECT round(exp(lgamma(10))) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query shows that gamma(200) returns infinity, while lgamma(200) returns a finite result, rounded to four decimal places.
SELECT isinf(gamma(200)) AS gamma_overflows, round(lgamma(200), 4) AS log_gamma
The query returns the following result.
| gamma_overflows | log_gamma |
|---|---|
|
|
Considerations
Because lgamma uses the absolute value of the gamma function, the result does not show its sign. For example, gamma(-0.5) is negative, and lgamma(-0.5) is the logarithm of its absolute value.
For 0 and negative integers, lgamma returns infinity, and the query does not fail.
The result is a floating-point approximation. Round exp(lgamma( before you compare it with an integer.n))