FLINT3.7.0-dev
Module · 20 functions

bernoulli.h

#include <flint/bernoulli.h>

This module computes the Bernoulli numbers, defined by the exponential generating function

xex−1=∑n=0∞Bnxnn!,\frac{x}{e^x - 1} = \sum_{n=0}^{\infty} B_n \frac{x^n}{n!},

as exact fractions: B0=1B_0 = 1, B1=−1/2B_1 = -1/2, B2=1/6B_2 = 1/6, and Bn=0B_n = 0 for odd n≥3n \ge 3. It provides isolated values (from the Riemann zeta function, or for very large nn combined with Harvey's multimodular algorithm), BnB_n modulo a prime, ranges of consecutive values generated in reverse order, and a cache used by the special functions of arb and acb (gamma function, zeta function, Euler–Maclaurin summation) that need many Bernoulli numbers repeatedly. The arith functions arith_bernoulli_number and arith_bernoulli_number_vec are wrappers and alternatives.

All algorithms are described in Bernoulli numbers, and Isolated Bernoulli numbers explains the choice of method for one value.

AlgorithmBernoulli numbersComputes exact Bernoulli numbers from the zeta function (numerically, with the denominator from von Staudt–Clausen), from Harvey's multimodular algorithm, by a reverse zeta iterator with a table of powers for ranges, and by Ramanujan's recursion or power series modulo primes for short vectors.

Isolated Bernoulli numbers#

Set the result to BnB_n in lowest terms with a positive denominator. The value is copied from the cache if it is there; the cache is not extended. Otherwise odd nn and n<18000n < 18000 use _bernoulli_fmpq_ui_zeta, and larger even nn use _bernoulli_fmpq_ui_multi_mod with the default tuning.

Full documentation
void _bernoulli_fmpq_ui_multi_mod(fmpz_t num, fmpz_t den, ulong n, double alpha)

Set num and den to the numerator and denominator of BnB_n in lowest terms, by two methods.

Full documentation

Returns Bn mod pB_n \bmod p for a prime p<2FLINT_D_BITSp < 2^{\mathtt{FLINT\_D\_BITS}} (2532^{53} on 64-bit machines), computed with Harvey's algorithm [Har2010] in time O(plog⁡O(1)p)O(p \log^{O(1)} p), independent of nn. If pp divides the denominator of BnB_n (that is, n≥2n \ge 2 even with (p−1)∣n(p - 1) \mid n, or n=1n = 1 and p=2p = 2), returns UWORD_MAX.

Full documentation

Ranges of Bernoulli numbers#

An iterator that generates the even-indexed Bernoulli numbers Bn,Bn−2,Bn−4,…,B0B_n, B_{n-2}, B_{n-4}, \ldots, B_0 in decreasing order, without caching. An array of length one of bernoulli_rev_struct.

More

Initialises iter so that the first call to bernoulli_rev_next returns BnB_n. An odd nn is replaced by n−1n - 1.

Sets numer and denom to the numerator and denominator of BkB_k in lowest terms, where BkB_k is the next number of the sequence, and advances iter to Bk−2B_{k-2}. Must not be called after B0B_0 has been returned.

Frees the memory used by iter.

Sets res to the num consecutive Bernoulli numbers Ba,Ba+1,…,Ba+num−1B_a, B_{a+1}, \ldots, B_{a + \mathit{num} - 1}, without reading or writing the cache. To reuse a vector, use bernoulli_cache_compute and bernoulli_fmpq_ui instead. Throws if a>231a > 2^{31} or num>109\mathit{num} > 10^9.

More

Cache#

The cache holds B0,…,BN−1B_0, \ldots, B_{N-1} as an array of fmpq. It is thread-local if FLINT is built with thread-local storage, so that each thread has its own copy, and it is freed by flint_cleanup.

The number NN of cached values and the array of cached values B0,…,BN−1B_0, \ldots, B_{N-1}.

Makes sure that B0,…,Bn−1B_0, \ldots, B_{n-1} are cached. The cache grows by at least 32 entries (below 128) or 128 entries at a time; new entries are computed with arith_bernoulli_number_vec up to 128 and with bernoulli_fmpq_vec_no_cache beyond.

Makes sure that BnB_n is cached, by calling bernoulli_cache_compute(n + 1) if needed.

Bounds#

Returns an integer bb with ∣Bn∣≤2b|B_n| \le 2^b, using a table for n<512n < 512 and otherwise the inequality ∣Bn∣<4 n!/(2π)n<4(n+1)n+1e−n/(2π)n|B_n| < 4\, n!/(2\pi)^n < 4 (n+1)^{n+1} e^{-n}/(2\pi)^n evaluated in integer arithmetic with a table of logarithms. Returns WORD_MIN if Bn=0B_n = 0. The bound exceeds the true exponent by at most one percent. Throws if n>2FLINT_BITS−6n > 2^{\mathtt{FLINT\_BITS} - 6} or the computation would overflow; the function is meant for nn small enough that BnB_n could be computed exactly.

Internal functions#

These inline helpers estimate the precision and number of terms for the zeta series used by the reverse iterator.

Returns a crude estimate of the number of bits of the denominator of BnB_n: by von Staudt–Clausen the denominator is at most nσ0(n)n^{\sigma_0(n)}, and the estimate is 12log⁡2(n) n1.066/log⁡log⁡n\tfrac12 \log_2(n)\, n^{1.066/\log\log n}, using σ0(n)<n1.066/log⁡log⁡n\sigma_0(n) < n^{1.066/\log\log n} for large nn.

Returns the working precision of the iterator for BnmaxB_{\mathit{nmax}}: arith_bernoulli_number_size(nmax) + bernoulli_denom_size(nmax). No guard bits are added, as the denominator estimate is pessimistic.

Returns the number NN of terms of the zeta series ∑j≤Nj−s\sum_{j \le N} j^{-s} needed for an error below 2−prec2^{-\mathit{prec}}: about 2(prec+1)/(s−1)2^{(\mathit{prec} + 1)/(s - 1)}, made odd.

Returns the precision for the term i−s1i^{-s_1} in a sum computed to absolute precision wp\mathit{wp}: wp−s1log⁡2i\mathit{wp} - s_1 \log_2 i, at least 10.

The reverse iterator uses a table for n<n < BERNOULLI_REV_MIN (32).

References#

  • [Har2010] D. Harvey, "A multimodular algorithm for computing Bernoulli numbers" (2010), Mathematics of Computation 79.272: 2361-2370
Cite this page

The FLINT team, “bernoulli.h (module overview),” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/bernoulli/index.html (generated 2026-11-02).