View a markdown version of this page

Factorial and gamma functions - Amazon Athena

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(x) returns the same result as x!.

Syntax

x! factorial(x)

Arguments

The ! (factorial) operator takes the following argument:

x

The number to compute the factorial of, as an INTEGER value from 0 to 33. A TINYINT, SMALLINT, UTINYINT, or USMALLINT value is converted to INTEGER. Cast a value of any other numeric type, such as BIGINT, UINTEGER, DECIMAL, or DOUBLE, to INTEGER first.

Return type

HUGEINT

Examples

The following SQL query returns the factorial of 5.

SELECT 5! AS result

The query returns the following result.

result

120

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

24

1

NULL

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

51090942171709440000

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

-6

12

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

10

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

2598960

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

0

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(n) equals the factorial of n - 1, so gamma(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 DOUBLE value. Values of other numeric types are converted to DOUBLE.

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

52.34277778455352

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

24.0

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

true

true

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(x). The logarithm grows much more slowly than the gamma function, so 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(n)) is approximately the factorial of n - 1.

Syntax

lgamma(x)

Arguments

The lgamma function takes the following argument:

x

The input to the gamma function, as a DOUBLE value. Values of other numeric types are converted to DOUBLE.

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

0.0

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

362880.0

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

true

857.9337

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(n)) before you compare it with an integer.