Stirling and Bell numbers
- Cost
- An isolated S(n,k) or B_n costs about bit operations; a row of Stirling numbers or a vector of Bell numbers modulo a word-size prime costs word operations.
The Stirling numbers and and the Bell numbers are defined on Stirling numbers and Bell numbers. All of these numbers have bits for typical , so the best possible cost of an isolated value is quasi-linear in ; the methods below are simpler and, apart from the binary splitting method for , cost about word operations, which is adequate up to of the order of . 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:
A row is computed by binary splitting: the product of the linear factors , , split recursively in halves, with fast polynomial multiplication at the top levels. The coefficients of the product of the factors with have at most bits, so the base cases with small products are done in single words, and the total cost is bit operations, quasi-linear in the size of the row.
For one coefficient , only a truncated product is needed: for the coefficients up to of , and otherwise the coefficients up to of the reversed product . The last multiplication of the binary splitting tree computes only the wanted coefficient, as a dot product. For very close to the exponential generating function
is cheaper, since the power series has length only ; FLINT uses it for and . The signed numbers are .
Stirling numbers of the second kind#
By inclusion–exclusion over the empty blocks,
a sum of terms of about bits. Two observations reduce the work. The powers are needed only for odd : for with odd, , a shift. And the binomial coefficients are symmetric, so only half of them are computed. Modulo a word-size prime the powers of composite are products of the powers of two factors, read from a divisor table, so that only exponentiations are needed; the sum is then a dot product.
Isolated values use:
- the triangular recurrence in one or two words while fits, that is for on 64-bit machines;
- the exact power sum for , where the terms are small;
the exponential generating function
for , a power of a series of length ;
- the power sum modulo bit primes followed by Chinese remaindering in between.
The cutoffs are for ; for larger , is (for ) or , kept in , and . The number of primes is determined by the bound
of Rennie and Dobson [RenDob1969].
A row is a convolution of the sequences and :
FLINT uses the triangular recurrence for , the convolution over with the common denominator , , for , 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 and exchanging the sums gives
and letting the inner sum run to infinity gives Dobiński's formula [Dob1877]
Dobiński's formula, exactly. With and , both integers,
For each has , so ; and makes the tail small enough that . Hence , computed with integer arithmetic only (arith_bell_number_dobinski). The terms are accumulated by Horner's rule in , so the cost is multiplications of a large number by a word plus the powers for odd , about bit operations.
Multimodular sum. Modulo a prime , the double sum above is evaluated with multiplications: the factorials are precomputed as , so that the inner sums are prefix sums and only one inversion (of ) is needed at the end, and the powers of composite come from a divisor table. FLINT accumulates the products in three words and reduces once, uses Montgomery arithmetic (Montgomery multiplication (REDC)) with primes of bits, and processes the primes in parallel (arith_bell_number_multi_mod). The number of primes comes from de Bruijn's asymptotic expansion [deB1958]
Vectors. The Bell triangle (Aitken's array) , , has and needs additions for . Modulo there are two power series methods: the exponential generating function , computed with one power series exponential in operations if is invertible modulo , and the ordinary generating function
which follows from . Truncated at , the sum is a fraction that binary splitting computes as polynomials of degree , followed by one power series division: operations, for any modulus.
Use in FLINT#
| Function | Method |
|---|---|
arith_stirling_number_1u, arith_stirling_number_1 | special cases; truncated binary splitting; EGF for , |
arith_stirling_number_2 | special cases; triangle for ; power sum, EGF or multimodular power sum by |
arith_stirling_number_1u_vec, arith_stirling_number_1_vec | binary splitting of the rising factorial |
arith_stirling_number_2_vec | triangle for ; convolution over for ; multimodular convolution |
arith_stirling_matrix_1u, arith_stirling_matrix_2, ... | gr_mat_stirling: triangular recurrences row by row |
arith_bell_number | table (); triangle (); Dobiński (); multimodular |
arith_bell_number_vec | triangle for ; multimodular vector |
arith_bell_number_nmod | table; periodicity for ; the sum if and is invertible; else the vector |
arith_bell_number_nmod_vec | triangle for ; EGF series if and invertible; triangle for ; 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 (and arith_bell_number_nmod and arith_bell_number_nmod_vec over ) and convert the result for other rings.
Bell numbers modulo 2 and 3 are periodic with periods 3 and 13, by Touchard's congruence for prime [Comtet1974].
void arith_stirling_number_2(fmpz_t s, ulong n, ulong k)void arith_bell_number(fmpz_t b, ulong n)void arith_bell_number_nmod_vec(nn_ptr b, slong n, nmod_t mod)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 für ", 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).
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).
@misc{flint-doc-algorithms-number-theory-stirling-bell,
author = {{The FLINT team}},
title = {{Stirling and Bell numbers}},
howpublished = {FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev},
year = {2026},
url = {https://flintlib.org/doc/algorithms/number-theory/stirling-bell.html},
note = {Generated 2026-11-02}
}