Counting

Counting

How many ways are there? Each classical answer is a function here, returning an exact integer of any size.

Arrangements and selections

Choosing kk things from nn splits four ways, by whether order matters and whether repeats are allowed:

Order mattersOrder does not matter
No repeatsn(n−1)⋯(n−k+1)n(n-1)\cdots(n-k+1)
fallingFactorial(n, k)
(nk)=n!k! (n−k)!\binom{n}{k} = \dfrac{n!}{k!\,(n-k)!}
binomial(n, k)
Repeats allowednkn^k
BigInt(n) ** BigInt(k)
(n+k−1k)\binom{n+k-1}{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 kk is outside 0…n0 \dots n, which is what makes sums over all kk come out right, and for negative nn it is the coefficient in the binomial series, (−1)k(k−n−1k)(-1)^k\binom{k-n-1}{k}.

The multichoose formula is the "stars and bars" argument: a selection of kk from nn kinds is a row of kk stars and n−1n - 1 bars, and there are (n+k−1k)\binom{n+k-1}{k} such rows.

Partitions of a set

The Stirling number of the second kind S(n,k)S(n, k) counts the ways to split nn distinct things into kk non-empty unlabelled groups; the Bell number BnB_n allows any number of groups. Labelling the groups gives the surjections, k! S(n,k)k!\,S(n, k).

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: S(n,k)=S(n−1,k−1)+k S(n−1,k)S(n, k) = S(n-1, k-1) + k\,S(n-1, k) (the new element is alone, or joins one of kk groups) and c(n,k)=c(n−1,k−1)+(n−1) c(n−1,k)c(n, k) = c(n-1, k-1) + (n-1)\,c(n-1, k) (it is a cycle of its own, or is inserted after one of the n−1n - 1 others). They are the change of basis between powers and falling factorials: xn=∑kS(n,k) x(x−1)⋯(x−k+1)x^n = \sum_k S(n, k)\,x(x-1)\cdots(x-k+1).

Partitions of a number

A partition of nn 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 p(n)p(n). It is computed from Euler's pentagonal number theorem, p(n)=∑j≥1(−1)j+1(p(n−j(3j−1)2)+p(n−j(3j+1)2))p(n) = \sum_{j \ge 1} (-1)^{j+1}\big(p(n - \tfrac{j(3j-1)}{2}) + p(n - \tfrac{j(3j+1)}{2})\big), which needs only about n\sqrt n 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 F2k=Fk(2Fk+1−Fk)F_{2k} = F_k(2F_{k+1} - F_k) and F2k+1=Fk2+Fk+12F_{2k+1} = F_k^2 + F_{k+1}^2, so FnF_n takes about log⁡2n\log_2 n steps: fibonacci(1000000) is a fraction of a second. The Catalan number 1n+1(2nn)\frac{1}{n+1}\binom{2n}{n} counts more than two hundred known families; Enumeration lists one of them.

Try itEdit the code and run it with @ratmath/discrete and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs