# knotkit/combinatorics

> Counts permutations, combinations, cycles, and set partitions exactly or in log space

## Use

```ts
import { logBinomialCoefficient, stirlingFirstKindRow, stirlingSecondKind } from 'knotkit/combinatorics'

stirlingFirstKindRow(4) // [0n, 6n, 11n, 6n, 1n]
stirlingSecondKind(4, 2) // 7n partitions into two blocks
logBinomialCoefficient(1e15, 1) // 34.538776394910684
```

## What it solves

* count permutations or unordered combinations exactly
* compute Stirling numbers for permutations by cycle count and partitions by block count
* compute Bell numbers to count all set partitions
* evaluate log-factorials and log binomial coefficients for large inputs

## Worked example

* 20 items give 2432902008176640000 permutations, 51724158235372 partitions, or 184756 subsets of size 10.
* Use the exact BigInt functions for counts and the log functions when you only need their magnitude.

Hover or focus a chart for exact values. Drawn by [`demo.ts`](https://github.com/jkomyno/knotkit/blob/main/packages/combinatorics/demo.ts) against `knotkit/combinatorics`.

## API

| Export                                                                                         | Returns             | Does                                                                                                      |
| ---------------------------------------------------------------------------------------------- | ------------------- | --------------------------------------------------------------------------------------------------------- |
| [`factorial`](/reference/knotkit/combinatorics/functions/factorial/)                           | `bigint`            | Permutations of all n items.                                                                              |
| [`logFactorial`](/reference/knotkit/combinatorics/functions/logfactorial/)                     | `number`            | Natural logarithm of n factorial for any non-negative safe integer n.                                     |
| [`binomialCoefficient`](/reference/knotkit/combinatorics/functions/binomialcoefficient/)       | `bigint`            | Combinations of k from n items.                                                                           |
| [`logBinomialCoefficient`](/reference/knotkit/combinatorics/functions/logbinomialcoefficient/) | `number`            | Natural logarithm of the number of combinations of k from n items, including when n is far larger than k. |
| [`stirlingFirstKind`](/reference/knotkit/combinatorics/functions/stirlingfirstkind/)           | `bigint`            | Permutations of n items with k cycles, the unsigned Stirling number of the first kind.                    |
| [`stirlingFirstKindRow`](/reference/knotkit/combinatorics/functions/stirlingfirstkindrow/)     | `readonly bigint[]` | Permutations of n items for each cycle count from 0 to n.                                                 |
| [`logStirlingFirstKind`](/reference/knotkit/combinatorics/functions/logstirlingfirstkind/)     | `number`            | Natural logarithm of the number of permutations with k cycles.                                            |
| [`stirlingSecondKind`](/reference/knotkit/combinatorics/functions/stirlingsecondkind/)         | `bigint`            | Partitions of n items into k non-empty blocks.                                                            |
| [`stirlingSecondKindRow`](/reference/knotkit/combinatorics/functions/stirlingsecondkindrow/)   | `readonly bigint[]` | Partitions of n items for each non-empty block count from 0 to n.                                         |
| [`logStirlingSecondKind`](/reference/knotkit/combinatorics/functions/logstirlingsecondkind/)   | `number`            | Natural logarithm of the number of partitions into k non-empty blocks.                                    |
| [`bellNumber`](/reference/knotkit/combinatorics/functions/bellnumber/)                         | `bigint`            | Set partitions across all block counts.                                                                   |
| [`logBellNumber`](/reference/knotkit/combinatorics/functions/logbellnumber/)                   | `number`            | Natural logarithm of the number of set partitions across all block counts.                                |

Cost per member. Open a row for every operation.

`factorial` 1 members

| Member    | Cost                 |
| --------- | -------------------- |
| `compute` | O(n) multiplications |

`logFactorial` 1 members

| Member    | Cost |
| --------- | ---- |
| `compute` | O(1) |

`binomialCoefficient` 1 members

| Member    | Cost                                              |
| --------- | ------------------------------------------------- |
| `compute` | O(min(k, n - k)) arithmetic operations for k <= n |

`logBinomialCoefficient` 1 members

| Member    | Cost |
| --------- | ---- |
| `compute` | O(1) |

`stirlingFirstKind` 1 members

| Member    | Cost                                   |
| --------- | -------------------------------------- |
| `compute` | O(nk) arithmetic operations for k <= n |

`stirlingFirstKindRow` 1 members

| Member    | Cost                                                |
| --------- | --------------------------------------------------- |
| `compute` | O(n²) arithmetic operations and O(n) bigint storage |

`logStirlingFirstKind` 1 members

| Member    | Cost                                   |
| --------- | -------------------------------------- |
| `compute` | O(nk) arithmetic operations for k <= n |

`stirlingSecondKind` 1 members

| Member    | Cost                                   |
| --------- | -------------------------------------- |
| `compute` | O(nk) arithmetic operations for k <= n |

`stirlingSecondKindRow` 1 members

| Member    | Cost                                                |
| --------- | --------------------------------------------------- |
| `compute` | O(n²) arithmetic operations and O(n) bigint storage |

`logStirlingSecondKind` 1 members

| Member    | Cost                                   |
| --------- | -------------------------------------- |
| `compute` | O(nk) arithmetic operations for k <= n |

`bellNumber` 1 members

| Member    | Cost                                    |
| --------- | --------------------------------------- |
| `compute` | O(n²) additions and O(n) bigint storage |

`logBellNumber` 1 members

| Member    | Cost                                    |
| --------- | --------------------------------------- |
| `compute` | O(n²) additions and O(n) bigint storage |

## Errors

Every error this module throws is `CombinatoricsInvalidInputError`, with a `code` from a closed, exported list. Branch on the code; the message is for people.

* `INVALID_COUNT` `COUNT_TOO_LARGE`

## When not to use it

| If you need to…                                  | Use instead                                              |
| ------------------------------------------------ | -------------------------------------------------------- |
| list the permutations or combinations themselves | a generator that enumerates permutations or combinations |
| simulate selecting items without replacement     | [`knotkit/sampling`](/modules/sampling/)                 |

## Performance

Measured by the suite in [`packages/combinatorics/__tests__/bench`](https://github.com/jkomyno/knotkit/tree/main/packages/combinatorics/__tests__/bench). The numbers come from one machine, so run the suite on yours before deciding on speed alone.

## Prompt an agent

Prompt · knotkit/combinatoricsCopy prompt

```
Use knotkit/combinatorics for this task. If Knotkit is missing, follow https://knotkit.jkomyno.dev/getting-started.md#install-from-source. Import from 'knotkit/combinatorics'.

Exports:
- `factorial` → `bigint`: Permutations of all n items.
- `logFactorial` → `number`: Natural logarithm of n factorial for any non-negative safe integer n.
- `binomialCoefficient` → `bigint`: Combinations of k from n items.
- `logBinomialCoefficient` → `number`: Natural logarithm of the number of combinations of k from n items, including when n is far larger than k.
- `stirlingFirstKind` → `bigint`: Permutations of n items with k cycles, the unsigned Stirling number of the first kind.
- `stirlingFirstKindRow` → `readonly bigint[]`: Permutations of n items for each cycle count from 0 to n.
- `logStirlingFirstKind` → `number`: Natural logarithm of the number of permutations with k cycles.
- `stirlingSecondKind` → `bigint`: Partitions of n items into k non-empty blocks.
- `stirlingSecondKindRow` → `readonly bigint[]`: Partitions of n items for each non-empty block count from 0 to n.
- `logStirlingSecondKind` → `number`: Natural logarithm of the number of partitions into k non-empty blocks.
- `bellNumber` → `bigint`: Set partitions across all block counts.
- `logBellNumber` → `number`: Natural logarithm of the number of set partitions across all block counts.

Use something else when:
- list the permutations or combinations themselves → a generator that enumerates permutations or combinations
- simulate selecting items without replacement → knotkit/sampling

Errors are CombinatoricsInvalidInputError with a `code` property. Branch on `error.code`, never on the message.

Full page: https://knotkit.jkomyno.dev/modules/combinatorics.md
Types: node_modules/knotkit/combinatorics/ and https://knotkit.jkomyno.dev/reference/knotkit/combinatorics/
```

## See also

* [API reference](/reference/knotkit/combinatorics/)every type and signature
* [Capability record](https://github.com/jkomyno/knotkit/blob/main/packages/combinatorics/knotkit.capability.json)this page as JSON, also in the installed package
* [Edit this page on GitHub](https://github.com/jkomyno/knotkit/edit/main/packages/combinatorics/knotkit.capability.json)suggest a change to the source record
