FLINT3.7.0-dev
Algorithm

Stirling and Bell numbers

Cost
An isolated S(n,k) or B_n costs about O(n2log⁡O(1)n)O(n^2 \log^{O(1)} n) bit operations; a row of Stirling numbers or a vector of Bell numbers modulo a word-size prime costs O(M(n)log⁡n)O(M(n) \log n) word operations.

The Stirling numbers s(n,k)s(n,k) and S(n,k)S(n,k) and the Bell numbers BnB_n are defined on Stirling numbers and Bell numbers. All of these numbers have Θ(nlog⁡n)\Theta(n \log n) bits for typical kk, so the best possible cost of an isolated value is quasi-linear in nlog⁡nn \log n; the methods below are simpler and, apart from the binary splitting method for s(n,k)s(n,k), cost about n2n^2 word operations, which is adequate up to nn of the order of 10510^5. The references for the combinatorics are [Comtet1974] and [GKP1994, §6.1].

Stirling numbers of the first kind#

The unsigned numbers are the coefficients of the rising factorial:

∑k=0n∣s(n,k)∣ xk=x(x+1)⋯(x+n−1).\sum_{k=0}^n |s(n,k)|\, x^k = x(x+1)\cdots(x+n-1).

A row is computed by binary splitting: the product of the n−1n - 1 linear factors x+jx + j, 1≤j<n1 \le j < n, split recursively in halves, with fast polynomial multiplication at the top levels. The coefficients of the product of the factors with j∈[a,b)j \in [a, b) have at most (b−a)log⁡2max⁡(b,b−a)(b-a) \log_2 \max(b, b - a) bits, so the base cases with small products are done in single words, and the total cost is O(M(n2log⁡n)log⁡n)O(M(n^2 \log n) \log n) bit operations, quasi-linear in the size of the row.

For one coefficient ∣s(n,k)∣|s(n,k)|, only a truncated product is needed: for k<n/2k < n/2 the coefficients up to xk−1x^{k-1} of ∏j=1n−1(x+j)\prod_{j=1}^{n-1} (x + j), and otherwise the coefficients up to xn−kx^{n-k} of the reversed product ∏j=1n−1(1+jx)\prod_{j=1}^{n-1} (1 + jx). The last multiplication of the binary splitting tree computes only the wanted coefficient, as a dot product. For kk very close to nn the exponential generating function

∣s(n,k)∣=n!k![xn−k](−log⁡(1−x)x)k|s(n,k)| = \frac{n!}{k!} [x^{n-k}] \left(\frac{-\log(1-x)}{x}\right)^k

is cheaper, since the power series has length only n−k+1n - k + 1; FLINT uses it for n>140n > 140 and k>0.87nk > 0.87 n. The signed numbers are s(n,k)=(−1)n−k∣s(n,k)∣s(n,k) = (-1)^{n-k} |s(n,k)|.

Stirling numbers of the second kind#

By inclusion–exclusion over the empty blocks,

S(n,k)=1k!∑j=0k(−1)k−j(kj)jn,S(n,k) = \frac{1}{k!} \sum_{j=0}^{k} (-1)^{k-j} \binom{k}{j} j^n,

a sum of k+1k + 1 terms of about nlog⁡kn \log k bits. Two observations reduce the work. The powers jnj^n are needed only for odd jj: for j=2ej′j = 2^e j' with j′j' odd, jn=j′n⋅2enj^n = j'^n \cdot 2^{en}, a shift. And the binomial coefficients are symmetric, so only half of them are computed. Modulo a word-size prime the powers of composite jj are products of the powers of two factors, read from a divisor table, so that only π(k)\pi(k) exponentiations are needed; the sum is then a dot product.

Isolated values use:

  • the triangular recurrence S(n,k)=kS(n−1,k)+S(n−1,k−1)S(n,k) = k S(n-1, k) + S(n-1, k-1) in one or two words while S(n,k)S(n,k) fits, that is for n≤43n \le 43 on 64-bit machines;
  • the exact power sum for k≤c1nk \le c_1 n, where the terms are small;
  • the exponential generating function

    S(n,k)=n!k![xn−k](ex−1x)kS(n,k) = \frac{n!}{k!} [x^{n-k}] \left(\frac{e^x - 1}{x}\right)^k

    for k≥c2nk \ge c_2 n, a power of a series of length n−k+1n - k + 1;

  • the power sum modulo FLINT_BITS−1\mathtt{FLINT\_BITS} - 1 bit primes followed by Chinese remaindering in between.

The cutoffs are c1=c2=0.9c_1 = c_2 = 0.9 for n<200n < 200; for larger nn, c1c_1 is 0.95e−0.00022n0.95 e^{-0.00022 n} (for n<3000n < 3000) or 1500/n1500/n, kept in [0.0002,0.8][0.0002, 0.8], and c2=min⁡(0.92+0.005log⁡n,0.98)c_2 = \min(0.92 + 0.005 \log n, 0.98). The number of primes is determined by the bound

S(n,k)≤12(nk)kn−k(1≤k<n)S(n,k) \le \frac{1}{2} \binom{n}{k} k^{n-k} \qquad (1 \le k < n)

of Rennie and Dobson [RenDob1969].

A row S(n,0),…,S(n,klen−1)S(n, 0), \ldots, S(n, \mathit{klen} - 1) is a convolution of the sequences jn/j!j^n/j! and (−1)i/i!(-1)^i/i!:

S(n,k)=∑j=0kjnj!⋅(−1)k−j(k−j)!.S(n,k) = \sum_{j=0}^{k} \frac{j^n}{j!} \cdot \frac{(-1)^{k-j}}{(k-j)!}.

FLINT uses the triangular recurrence for n≤80n \le 80, the convolution over Z\mathbb{Z} with the common denominator ℓ!2\ell!^2, ℓ=min⁡(klen,n)−1\ell = \min(\mathit{klen}, n) - 1, for klen<n/2\mathit{klen} < n/2, and otherwise the convolution modulo word-size primes with Chinese remaindering, where each entry uses only as many primes as its own bound requires.

Bell numbers#

Summing the power sum formula over kk and exchanging the sums gives

Bn=∑k=0nS(n,k)=∑j=0njnj!∑i=0n−j(−1)ii!,B_n = \sum_{k=0}^{n} S(n, k) = \sum_{j=0}^n \frac{j^n}{j!} \sum_{i=0}^{n-j} \frac{(-1)^i}{i!},

and letting the inner sum run to infinity gives Dobiński's formula [Dob1877]

Bn=1e∑k=0∞knk!.B_n = \frac{1}{e} \sum_{k=0}^{\infty} \frac{k^n}{k!}.

Dobiński's formula, exactly. With P=N!∑k=0Nkn/k!P = N! \sum_{k=0}^N k^n/k! and Q=N!∑k=0N1/k!Q = N! \sum_{k=0}^N 1/k!, both integers,

Bn−PQ=TP−BnTQe−TQ,TP=∑k>Nknk!, TQ=∑k>N1k!.B_n - \frac{P}{Q} = \frac{T_P - B_n T_Q}{e - T_Q}, \qquad T_P = \sum_{k > N} \frac{k^n}{k!},\ T_Q = \sum_{k > N} \frac{1}{k!}.

For N≥nN \ge n each k>Nk > N has kn>nn>Bnk^n > n^n > B_n, so P/Q≤BnP/Q \le B_n; and N≈n(1+1.2/log⁡n)N \approx n(1 + 1.2/\log n) makes the tail TPT_P small enough that P/Q>Bn−1P/Q > B_n - 1. Hence Bn=⌈P/Q⌉B_n = \lceil P/Q \rceil, computed with integer arithmetic only (arith_bell_number_dobinski). The terms are accumulated by Horner's rule in kk, so the cost is O(N)O(N) multiplications of a large number by a word plus the N/2N/2 powers knk^n for odd kk, about O(n2log⁡2n)O(n^2 \log^2 n) bit operations.

Multimodular sum. Modulo a prime p>np > n, the double sum above is evaluated with O(n)O(n) multiplications: the factorials are precomputed as n!/i!n!/i!, so that the inner sums are prefix sums and only one inversion (of n!2n!^2) is needed at the end, and the powers jnj^n of composite jj come from a divisor table. FLINT accumulates the products in three words and reduces once, uses Montgomery arithmetic (Montgomery multiplication (REDC)) with primes of FLINT_BITS−3\mathtt{FLINT\_BITS} - 3 bits, and processes the primes in parallel (arith_bell_number_multi_mod). The number of primes comes from de Bruijn's asymptotic expansion [deB1958]

log⁡Bn=n(log⁡n−log⁡log⁡n−1+log⁡log⁡nlog⁡n+1log⁡n+12(log⁡log⁡nlog⁡n)2+O(log⁡log⁡nlog⁡2n)).\log B_n = n \left( \log n - \log \log n - 1 + \frac{\log\log n}{\log n} + \frac{1}{\log n} + \frac{1}{2} \left(\frac{\log\log n}{\log n}\right)^2 + O\left(\frac{\log\log n}{\log^2 n}\right) \right).

Vectors. The Bell triangle (Aitken's array) a0,0=1a_{0,0} = 1, ai,0=ai−1,i−1a_{i,0} = a_{i-1,i-1}, ai,j=ai,j−1+ai−1,j−1a_{i,j} = a_{i,j-1} + a_{i-1,j-1} has ai,0=Bia_{i,0} = B_i and needs n2/2n^2/2 additions for B0,…,Bn−1B_0, \ldots, B_{n-1}. Modulo mm there are two power series methods: the exponential generating function exp⁡(ex−1)\exp(e^x - 1), computed with one power series exponential in O(M(n))O(M(n)) operations if (n−1)!(n-1)! is invertible modulo mm, and the ordinary generating function

∑n≥0Bnxn=∑k≥0xk(1−x)(1−2x)⋯(1−kx),\sum_{n \ge 0} B_n x^n = \sum_{k \ge 0} \frac{x^k}{(1 - x)(1 - 2x) \cdots (1 - kx)},

which follows from ∑nS(n,k)xn=xk/∏j=1k(1−jx)\sum_n S(n,k) x^n = x^k / \prod_{j=1}^k (1 - jx). Truncated at k=n−1k = n - 1, the sum is a fraction R/QR/Q that binary splitting computes as polynomials of degree nn, followed by one power series division: O(M(n)log⁡n)O(M(n) \log n) operations, for any modulus.

Use in FLINT#

FunctionMethod
arith_stirling_number_1u, arith_stirling_number_1special cases; truncated binary splitting; EGF for n>140n > 140, k>0.87nk > 0.87 n
arith_stirling_number_2special cases; triangle for n≤43n \le 43; power sum, EGF or multimodular power sum by k/nk/n
arith_stirling_number_1u_vec, arith_stirling_number_1_vecbinary splitting of the rising factorial
arith_stirling_number_2_vectriangle for n≤80n \le 80; convolution over Z\mathbb{Z} for klen<n/2\mathit{klen} < n/2; multimodular convolution
arith_stirling_matrix_1u, arith_stirling_matrix_2, ...gr_mat_stirling: triangular recurrences row by row
arith_bell_numbertable (n≤25n \le 25); triangle (n≤58n \le 58); Dobiński (n≤3400n \le 3400); multimodular
arith_bell_number_vectriangle for n<5000n < 5000; multimodular vector
arith_bell_number_nmodtable; periodicity for m=2,3m = 2, 3; the O(n)O(n) sum if m>nm > n and n!n! is invertible; else the vector
arith_bell_number_nmod_vectriangle for n<300n < 300; EGF series if m≥nm \ge n and invertible; triangle for n<500+bits(m)2n < 500 + \mathit{bits}(m)^2; OGF

The generic ring functions gr_stirling_s1u_uiui, gr_stirling_s2_uiui, gr_stirling_s1_ui_vec (and the other kinds), gr_bellnum_ui and gr_bellnum_vec call these functions over Z\mathbb{Z} (and arith_bell_number_nmod and arith_bell_number_nmod_vec over Z/nZ\mathbb{Z}/n\mathbb{Z}) and convert the result for other rings.

Bell numbers modulo 2 and 3 are periodic with periods 3 and 13, by Touchard's congruence Bn+p≡Bn+Bn+1(modp)B_{n+p} \equiv B_n + B_{n+1} \pmod p for prime pp [Comtet1974].

Referenced from#

References#

  • [Comtet1974] L. Comtet, Advanced Combinatorics: The Art of Finite and Infinite Expansions, D. Reidel, 1974.
  • [GKP1994] R. L. Graham, D. E. Knuth and O. Patashnik, Concrete Mathematics, second edition, Addison-Wesley, 1994.
  • [RenDob1969] B. C. Rennie and A. J. Dobson, "On Stirling numbers of the second kind", Journal of Combinatorial Theory 7:2 (1969), 116–121. https://doi.org/10.1016/S0021-9800(69)80045-1
  • [Dob1877] G. Dobiński, "Summirung der Reihe ∑nm/n!\sum n^m/n! für m=1,2,3,4,5,…m = 1, 2, 3, 4, 5, \ldots", Archiv der Mathematik und Physik 61 (1877), 333–336.
  • [deB1958] N. G. de Bruijn, Asymptotic Methods in Analysis, North-Holland, 1958 (§6.2: Bell numbers).
Cite this page

The FLINT team, “Stirling and Bell numbers,” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/algorithms/number-theory/stirling-bell.html (generated 2026-11-02).