Skip to content

knotkit/combinatorics

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

import {
function logBinomialCoefficient(n: number, k: number): number

Take the natural logarithm of the number of combinations of k from n items for any non-negative safe integer counts. Returns -Infinity for k > n.

O(1) time. Evaluates the logarithmic ratio without subtracting two large log-factorials.

@throws ― {CombinatoricsInvalidInputError} With INVALID_COUNT for an invalid count.

logBinomialCoefficient
,
function stirlingFirstKindRow(n: number): readonly bigint[]

Return the unsigned Stirling numbers of the first kind for k from 0 to n, in order.

O(n²) arithmetic operations and O(n) bigint storage.

@throws ― {CombinatoricsInvalidInputError} With INVALID_COUNT for an invalid count or COUNT_TOO_LARGE if n exceeds COMBINATORICS_MAX_COUNT.

stirlingFirstKindRow
,
function stirlingSecondKind(n: number, k: number): bigint

Count partitions of n items into k non-empty blocks as a bigint. Returns 0n for k > n.

O(nk) arithmetic operations and O(k) bigint storage when k <= n.

@throws ― {CombinatoricsInvalidInputError} With INVALID_COUNT for an invalid count or COUNT_TOO_LARGE if n exceeds COMBINATORICS_MAX_COUNT.

stirlingSecondKind
} from 'knotkit/combinatorics'
function stirlingFirstKindRow(n: number): readonly bigint[]

Return the unsigned Stirling numbers of the first kind for k from 0 to n, in order.

O(n²) arithmetic operations and O(n) bigint storage.

@throws ― {CombinatoricsInvalidInputError} With INVALID_COUNT for an invalid count or COUNT_TOO_LARGE if n exceeds COMBINATORICS_MAX_COUNT.

stirlingFirstKindRow
(4) // [0n, 6n, 11n, 6n, 1n]
function stirlingSecondKind(n: number, k: number): bigint

Count partitions of n items into k non-empty blocks as a bigint. Returns 0n for k > n.

O(nk) arithmetic operations and O(k) bigint storage when k <= n.

@throws ― {CombinatoricsInvalidInputError} With INVALID_COUNT for an invalid count or COUNT_TOO_LARGE if n exceeds COMBINATORICS_MAX_COUNT.

stirlingSecondKind
(4, 2) // 7n partitions into two blocks
function logBinomialCoefficient(n: number, k: number): number

Take the natural logarithm of the number of combinations of k from n items for any non-negative safe integer counts. Returns -Infinity for k > n.

O(1) time. Evaluates the logarithmic ratio without subtracting two large log-factorials.

@throws ― {CombinatoricsInvalidInputError} With INVALID_COUNT for an invalid count.

logBinomialCoefficient
(1e15, 1) // 34.538776394910684
combinatorics.ts
  • 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
{"v":1,"box":[120.6493,94.1597,756.9333,437.3555],"x":{"s":"linear","lim":[1.19,20.81],"inv":false,"g":0,"t":"Number of items","tk":[[2,"2"],[5,"5"],[10,"10"],[15,"15"],[20,"20"]]},"y":{"s":"log","lim":[0.307044086629,15847248744500000000],"inv":false,"g":0,"tk":[[2,"2"],[1000,"1 thousand"],[1000000,"1 million"],[1000000000,"1 billion"],[1000000000000,"1 trillion"],[1000000000000000,"1 quadrillion"],[1000000000000000000,"1 quintillion"]],"b":10},"ti":"Orderings pass 1 billion at 13 items, partitions at 15, half-size subsets not by 20","a":[{"k":"line","c":"#fffff8","s":"x","d":[2,2,3,6,4,24,5,120,6,720,7,5040,8,40320,9,362880,10,3628800,11,39916800,12,479001600,13,6227020800,14,87178291200,15,1307674368000,16,20922789888000,17,355687428096000,18,6402373705730000,19,121645100409000000,20,2432902008180000000],"p":[146.92,423.19,179.35,414.88,211.78,404.4,244.21,392.23,276.64,378.68,309.07,363.97,341.5,348.25,373.93,331.63,406.36,314.22,438.79,296.09,471.22,277.3,503.65,257.91,536.08,237.96,568.51,217.48,600.94,196.52,633.37,175.1,665.8,153.24,698.23,130.98,730.66,108.33]},{"k":"line","c":"#1d5fa6","s":"x","d":[2,2,3,6,4,24,5,120,6,720,7,5040,8,40320,9,362880,10,3628800,11,39916800,12,479001600,13,6227020800,14,87178291200,15,1307674368000,16,20922789888000,17,355687428096000,18,6402373705730000,19,121645100409000000,20,2432902008180000000],"p":[146.92,423.19,179.35,414.88,211.78,404.4,244.21,392.23,276.64,378.68,309.07,363.97,341.5,348.25,373.93,331.63,406.36,314.22,438.79,296.09,471.22,277.3,503.65,257.91,536.08,237.96,568.51,217.48,600.94,196.52,633.37,175.1,665.8,153.24,698.23,130.98,730.66,108.33]},{"k":"line","c":"#fffff8","s":"x","d":[2,2,3,5,4,15,5,52,6,203,7,877,8,4140,9,21147,10,115975,11,678570,12,4213597,13,27644437,14,190899322,15,1382958545,16,10480142147,17,82864869804,18,682076806159,19,5832742205060,20,51724158235400],"p":[146.92,423.19,179.35,416.26,211.78,407.95,244.21,398.55,276.64,388.25,309.07,377.19,341.5,365.46,373.93,353.13,406.36,340.26,438.79,326.9,471.22,313.09,503.65,298.87,536.08,284.26,568.51,269.29,600.94,253.97,633.37,238.34,665.8,222.4,698.23,206.18,730.66,189.67]},{"k":"line","c":"#c4501b","s":"x","d":[2,2,3,5,4,15,5,52,6,203,7,877,8,4140,9,21147,10,115975,11,678570,12,4213597,13,27644437,14,190899322,15,1382958545,16,10480142147,17,82864869804,18,682076806159,19,5832742205060,20,51724158235400],"p":[146.92,423.19,179.35,416.26,211.78,407.95,244.21,398.55,276.64,388.25,309.07,377.19,341.5,365.46,373.93,353.13,406.36,340.26,438.79,326.9,471.22,313.09,503.65,298.87,536.08,284.26,568.51,269.29,600.94,253.97,633.37,238.34,665.8,222.4,698.23,206.18,730.66,189.67]},{"k":"line","c":"#fffff8","s":"x","d":[2,2,3,3,4,6,5,10,6,20,7,35,8,70,9,126,10,252,11,462,12,924,13,1716,14,3432,15,6435,16,12870,17,24310,18,48620,19,92378,20,184756],"p":[146.92,423.19,179.35,420.12,211.78,414.88,244.21,411.02,276.64,405.78,309.07,401.55,341.5,396.3,373.93,391.86,406.36,386.62,438.79,382.04,471.22,376.8,503.65,372.12,536.08,366.87,568.51,362.12,600.94,356.88,633.37,352.07,665.8,346.83,698.23,341.98,730.66,336.74]},{"k":"line","c":"#0b7f61","s":"x","d":[2,2,3,3,4,6,5,10,6,20,7,35,8,70,9,126,10,252,11,462,12,924,13,1716,14,3432,15,6435,16,12870,17,24310,18,48620,19,92378,20,184756],"p":[146.92,423.19,179.35,420.12,211.78,414.88,244.21,411.02,276.64,405.78,309.07,401.55,341.5,396.3,373.93,391.86,406.36,386.62,438.79,382.04,471.22,376.8,503.65,372.12,536.08,366.87,568.51,362.12,600.94,356.88,633.37,352.07,665.8,346.83,698.23,341.98,730.66,336.74]},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"line","c":"#8c8c84"},{"k":"annotation","c":"#5c5c57"},{"k":"annotation","c":"#5c5c57"},{"k":"annotation","c":"#1d5fa6"},{"k":"annotation","c":"#c4501b"},{"k":"annotation","c":"#0b7f61"}],"lg":[]} 2 5 10 15 20 Number of items 2 1 thousand 1 million 1 billion 1 trillion 1 quadrillion 1 quintillion one second at 1 ns per candidate Candidates to enumerate (log scale) Orderings, n! Partitions, Bell(n) Half-size subsets, C(n, n/2) Orderings pass 1 billion at 13 items, partitions at 15, half-size subsets not by 20
{"v":1,"box":[120.6493,150.6246,308.9333,437.3555],"x":{"s":"linear","lim":[1.19,20.81],"inv":false,"g":0,"t":"Number of items","tk":[[2,"2"],[5,"5"],[10,"10"],[15,"15"],[20,"20"]]},"y":{"s":"log","lim":[0.307044086629,15847248744500000000],"inv":false,"g":0,"tk":[[2,"2"],[1000,"1 thousand"],[1000000,"1 million"],[1000000000,"1 billion"],[1000000000000,"1 trillion"],[1000000000000000,"1 quadrillion"],[1000000000000000000,"1 quintillion"]],"b":10},"ti":"Orderings pass 1 billion at 13 items,\npartitions at 15, half-size subsets not\nby 20","a":[{"k":"line","c":"#fffff8","s":"x","d":[2,2,3,6,4,24,5,120,6,720,7,5040,8,40320,9,362880,10,3628800,11,39916800,12,479001600,13,6227020800,14,87178291200,15,1307674368000,16,20922789888000,17,355687428096000,18,6402373705730000,19,121645100409000000,20,2432902008180000000],"p":[128.42,425.52,138.02,418.58,147.62,409.82,157.21,399.65,166.81,388.34,176.41,376.04,186,362.91,195.6,349.03,205.19,334.48,214.79,319.33,224.39,303.64,233.98,287.43,243.58,270.76,253.18,253.66,262.77,236.14,272.37,218.24,281.97,199.99,291.56,181.39,301.16,162.46]},{"k":"line","c":"#1d5fa6","s":"x","d":[2,2,3,6,4,24,5,120,6,720,7,5040,8,40320,9,362880,10,3628800,11,39916800,12,479001600,13,6227020800,14,87178291200,15,1307674368000,16,20922789888000,17,355687428096000,18,6402373705730000,19,121645100409000000,20,2432902008180000000],"p":[128.42,425.52,138.02,418.58,147.62,409.82,157.21,399.65,166.81,388.34,176.41,376.04,186,362.91,195.6,349.03,205.19,334.48,214.79,319.33,224.39,303.64,233.98,287.43,243.58,270.76,253.18,253.66,262.77,236.14,272.37,218.24,281.97,199.99,291.56,181.39,301.16,162.46]},{"k":"line","c":"#fffff8","s":"x","d":[2,2,3,5,4,15,5,52,6,203,7,877,8,4140,9,21147,10,115975,11,678570,12,4213597,13,27644437,14,190899322,15,1382958545,16,10480142147,17,82864869804,18,682076806159,19,5832742205060,20,51724158235400],"p":[128.42,425.52,138.02,419.73,147.62,412.79,157.21,404.94,166.81,396.33,176.41,387.09,186,377.29,195.6,366.98,205.19,356.23,214.79,345.07,224.39,333.54,233.98,321.66,243.58,309.45,253.18,296.94,262.77,284.15,272.37,271.08,281.97,257.77,291.56,244.21,301.16,230.42]},{"k":"line","c":"#c4501b","s":"x","d":[2,2,3,5,4,15,5,52,6,203,7,877,8,4140,9,21147,10,115975,11,678570,12,4213597,13,27644437,14,190899322,15,1382958545,16,10480142147,17,82864869804,18,682076806159,19,5832742205060,20,51724158235400],"p":[128.42,425.52,138.02,419.73,147.62,412.79,157.21,404.94,166.81,396.33,176.41,387.09,186,377.29,195.6,366.98,205.19,356.23,214.79,345.07,224.39,333.54,233.98,321.66,243.58,309.45,253.18,296.94,262.77,284.15,272.37,271.08,281.97,257.77,291.56,244.21,301.16,230.42]},{"k":"line","c":"#fffff8","s":"x","d":[2,2,3,3,4,6,5,10,6,20,7,35,8,70,9,126,10,252,11,462,12,924,13,1716,14,3432,15,6435,16,12870,17,24310,18,48620,19,92378,20,184756],"p":[128.42,425.52,138.02,422.96,147.62,418.58,157.21,415.35,166.81,410.97,176.41,407.44,186,403.06,195.6,399.35,205.19,394.97,214.79,391.14,224.39,386.76,233.98,382.85,243.58,378.47,253.18,374.5,262.77,370.12,272.37,366.1,281.97,361.72,291.56,357.67,301.16,353.29]},{"k":"line","c":"#0b7f61","s":"x","d":[2,2,3,3,4,6,5,10,6,20,7,35,8,70,9,126,10,252,11,462,12,924,13,1716,14,3432,15,6435,16,12870,17,24310,18,48620,19,92378,20,184756],"p":[128.42,425.52,138.02,422.96,147.62,418.58,157.21,415.35,166.81,410.97,176.41,407.44,186,403.06,195.6,399.35,205.19,394.97,214.79,391.14,224.39,386.76,233.98,382.85,243.58,378.47,253.18,374.5,262.77,370.12,272.37,366.1,281.97,361.72,291.56,357.67,301.16,353.29]},{"k":"segments","c":"#8c8c84"},{"k":"segments","c":"#8c8c84"},{"k":"line","c":"#8c8c84"},{"k":"annotation","c":"#5c5c57"},{"k":"annotation","c":"#5c5c57"},{"k":"annotation","c":"#1d5fa6"},{"k":"annotation","c":"#c4501b"},{"k":"annotation","c":"#0b7f61"}],"lg":[]} 2 5 10 15 20 Number of items 2 1 thousand 1 million 1 billion 1 trillion 1 quadrillion 1 quintillion one second at 1 ns per candidate Candidates to enumerate (log scale) Orderings, n! Partitions, Bell(n) Half-size subsets, C(n, n/2) Orderings pass 1 billion at 13 items, partitions at 15, half-size subsets not by 20
  • 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 against knotkit/combinatorics.

ExportReturnsDoes
factorialbigintPermutations of all n items.
logFactorialnumberNatural logarithm of n factorial for any non-negative safe integer n.
binomialCoefficientbigintCombinations of k from n items.
logBinomialCoefficientnumberNatural logarithm of the number of combinations of k from n items, including when n is far larger than k.
stirlingFirstKindbigintPermutations of n items with k cycles, the unsigned Stirling number of the first kind.
stirlingFirstKindRowreadonly bigint[]Permutations of n items for each cycle count from 0 to n.
logStirlingFirstKindnumberNatural logarithm of the number of permutations with k cycles.
stirlingSecondKindbigintPartitions of n items into k non-empty blocks.
stirlingSecondKindRowreadonly bigint[]Partitions of n items for each non-empty block count from 0 to n.
logStirlingSecondKindnumberNatural logarithm of the number of partitions into k non-empty blocks.
bellNumberbigintSet partitions across all block counts.
logBellNumbernumberNatural logarithm of the number of set partitions across all block counts.

Cost per member. Open a row for every operation.

factorial 1 members
MemberCost
computeO(n) multiplications
logFactorial 1 members
MemberCost
computeO(1)
binomialCoefficient 1 members
MemberCost
computeO(min(k, n - k)) arithmetic operations for k <= n
logBinomialCoefficient 1 members
MemberCost
computeO(1)
stirlingFirstKind 1 members
MemberCost
computeO(nk) arithmetic operations for k <= n
stirlingFirstKindRow 1 members
MemberCost
computeO(n²) arithmetic operations and O(n) bigint storage
logStirlingFirstKind 1 members
MemberCost
computeO(nk) arithmetic operations for k <= n
stirlingSecondKind 1 members
MemberCost
computeO(nk) arithmetic operations for k <= n
stirlingSecondKindRow 1 members
MemberCost
computeO(n²) arithmetic operations and O(n) bigint storage
logStirlingSecondKind 1 members
MemberCost
computeO(nk) arithmetic operations for k <= n
bellNumber 1 members
MemberCost
computeO(n²) additions and O(n) bigint storage
logBellNumber 1 members
MemberCost
computeO(n²) additions and O(n) bigint storage

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
If you need to…Use instead
list the permutations or combinations themselvesa generator that enumerates permutations or combinations
simulate selecting items without replacementknotkit/sampling

Measured by the suite in packages/combinatorics/__tests__/bench. The numbers come from one machine, so run the suite on yours before deciding on speed alone.

Prompt · knotkit/combinatorics
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/