

# 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
<a name="athena-duckdb-functions-numeric-factorial-operator"></a>

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
<a name="athena-duckdb-functions-numeric-factorial-operator-syntax"></a>

```sql
{{x}}!
factorial({{x}})
```

### Arguments
<a name="athena-duckdb-functions-numeric-factorial-operator-arguments"></a>

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
<a name="athena-duckdb-functions-numeric-factorial-operator-return-type"></a>

`HUGEINT`

### Examples
<a name="athena-duckdb-functions-numeric-factorial-operator-examples"></a>

The following SQL query returns the factorial of `5`.

```sql
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`.

```sql
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.

```sql
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.

```sql
SELECT -3! AS sign_after, 2 * 3! AS product
```

The query returns the following result.


| sign\_after | product | 
| --- | --- | 
| `-6` | `12` | 

### Considerations
<a name="athena-duckdb-functions-numeric-factorial-operator-considerations"></a>

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
<a name="athena-duckdb-functions-numeric-binom"></a>

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
<a name="athena-duckdb-functions-numeric-binom-syntax"></a>

```sql
binom({{n}}, {{k}})
```

### Arguments
<a name="athena-duckdb-functions-numeric-binom-arguments"></a>

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
<a name="athena-duckdb-functions-numeric-binom-return-type"></a>

`HUGEINT`

### Examples
<a name="athena-duckdb-functions-numeric-binom-examples"></a>

The following SQL query returns the number of ways to choose 2 items from 5.

```sql
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.

```sql
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`.

```sql
SELECT binom(5, 6) AS result
```

The query returns the following result.


| result | 
| --- | 
| `0` | 

### Considerations
<a name="athena-duckdb-functions-numeric-binom-considerations"></a>

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
<a name="athena-duckdb-functions-numeric-gamma"></a>

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](#athena-duckdb-functions-numeric-lgamma), which returns the natural logarithm of the gamma function and stays finite.

### Syntax
<a name="athena-duckdb-functions-numeric-gamma-syntax"></a>

```sql
gamma({{x}})
```

### Arguments
<a name="athena-duckdb-functions-numeric-gamma-arguments"></a>

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
<a name="athena-duckdb-functions-numeric-gamma-return-type"></a>

`DOUBLE`

### Examples
<a name="athena-duckdb-functions-numeric-gamma-examples"></a>

The following SQL query returns the gamma function of `5.5`.

```sql
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`.

```sql
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.

```sql
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
<a name="athena-duckdb-functions-numeric-gamma-considerations"></a>

`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](#athena-duckdb-functions-numeric-lgamma).

## `lgamma` function
<a name="athena-duckdb-functions-numeric-lgamma"></a>

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
<a name="athena-duckdb-functions-numeric-lgamma-syntax"></a>

```sql
lgamma({{x}})
```

### Arguments
<a name="athena-duckdb-functions-numeric-lgamma-arguments"></a>

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
<a name="athena-duckdb-functions-numeric-lgamma-return-type"></a>

`DOUBLE`

### Examples
<a name="athena-duckdb-functions-numeric-lgamma-examples"></a>

The following SQL query returns the logarithm of the gamma function of `2`. `gamma(2)` is `1`, so the result is `0`.

```sql
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.

```sql
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.

```sql
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
<a name="athena-duckdb-functions-numeric-lgamma-considerations"></a>

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.