Counting
How many ways are there? Each classical answer is a function here, returning an exact integer of any size.
Arrangements and selections
Choosing things from splits four ways, by whether order matters and whether repeats are allowed:
| Order matters | Order does not matter | |
|---|---|---|
| No repeats | fallingFactorial(n, k) | binomial(n, k) |
| Repeats allowed | BigInt(n) ** BigInt(k) | multichoose(n, k) |
import { factorial, fallingFactorial, binomial, multichoose, multinomial, pascalRow } from "@ratmath/discrete";
factorial(20); // 2432902008176640000n
fallingFactorial(10, 3); // 720n: gold, silver, bronze among ten runners
binomial(52, 5); // 2598960n: poker hands
multichoose(3, 10); // 66n: ten scoops from three flavours
multinomial(4, 4, 2, 1); // 34650n: arrangements of the letters of MISSISSIPPI
pascalRow(6); // [1n, 6n, 15n, 20n, 15n, 6n, 1n]
binomial(-3, 2); // 6n: the coefficient of x² in (1 + x)⁻³
Arguments are whole numbers as numbers, BigInts, digit strings or Core Rationals with denominator 1. Results are always BigInt. binomial(n, k) is 0 when is outside , which is what makes sums over all come out right, and for negative it is the coefficient in the binomial series, .
The multichoose formula is the "stars and bars" argument: a selection of from kinds is a row of stars and bars, and there are such rows.
Partitions of a set
The Stirling number of the second kind counts the ways to split distinct things into non-empty unlabelled groups; the Bell number allows any number of groups. Labelling the groups gives the surjections, .
import { stirling2, bell, surjections, stirling1 } from "@ratmath/discrete";
stirling2(5, 2); // 15n
bell(5); // 52n = 1 + 15 + 25 + 10 + 1
surjections(4, 2); // 14n: functions from 4 things onto 2
stirling1(5, 2); // 50n: permutations of 5 with exactly 2 cycles
stirling1(5, 2, { signed: true }); // -50n: the coefficient of x² in x(x−1)(x−2)(x−3)(x−4)
Both kinds of Stirling numbers come from a triangle like Pascal's: (the new element is alone, or joins one of groups) and (it is a cycle of its own, or is inserted after one of the others). They are the change of basis between powers and falling factorials: .
Partitions of a number
A partition of is a way to write it as a sum of positive whole numbers, order ignored; a composition counts the order.
import { partitionCount, compositionCount } from "@ratmath/discrete";
partitionCount(5); // 7n: 5, 4+1, 3+2, 3+1+1, 2+2+1, 2+1+1+1, 1+1+1+1+1
partitionCount(200); // 3972999029388n
partitionCount(8, 3); // 5n: into exactly three parts
compositionCount(5); // 16n = 2⁴
compositionCount(6, 3); // 10n = C(5, 2)
There is no simple formula for . It is computed from Euler's pentagonal number theorem, , which needs only about earlier values for each new one.
Named sequences
import { catalan, fibonacci, lucas, derangementCount, eulerian } from "@ratmath/discrete";
catalan(4); // 14n: balanced strings of 4 pairs of brackets, binary trees with 4 nodes, …
fibonacci(100); // 354224848179261915075n
fibonacci(-7); // 13n: F(−n) = (−1)^(n+1) F(n)
lucas(10); // 123n
derangementCount(5); // 44n: permutations with no fixed point
eulerian(4, 1); // 11n: permutations of 4 with exactly one descent
Fibonacci numbers use the doubling identities and , so takes about steps: fibonacci(1000000) is a fraction of a second. The Catalan number counts more than two hundred known families; Enumeration lists one of them.
@ratmath/discrete and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.