bernoulli.h
#include <flint/bernoulli.h>This module computes the Bernoulli numbers, defined by the exponential generating function
as exact fractions: , , , and for odd . It provides isolated values (from the Riemann zeta function, or for very large combined with Harvey's multimodular algorithm), 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#
void bernoulli_fmpq_ui(fmpq_t b, ulong n)void _bernoulli_fmpq_ui(fmpz_t num, fmpz_t den, ulong n)Set the result to 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 and use _bernoulli_fmpq_ui_zeta, and larger even use _bernoulli_fmpq_ui_multi_mod with the default tuning.
void _bernoulli_fmpq_ui_zeta(fmpz_t num, fmpz_t den, ulong n)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 in lowest terms, by two methods.
Returns for a prime ( on 64-bit machines), computed with Harvey's algorithm [Har2010] in time , independent of . If divides the denominator of (that is, even with , or and ), returns UWORD_MAX.
Ranges of Bernoulli numbers#
An iterator that generates the even-indexed Bernoulli numbers in decreasing order, without caching. An array of length one of bernoulli_rev_struct.
void bernoulli_rev_init(bernoulli_rev_t iter, ulong n)Initialises iter so that the first call to bernoulli_rev_next returns . An odd is replaced by .
void bernoulli_rev_next(fmpz_t numer, fmpz_t denom, bernoulli_rev_t iter)Sets numer and denom to the numerator and denominator of in lowest terms, where is the next number of the sequence, and advances iter to . Must not be called after has been returned.
void bernoulli_rev_clear(bernoulli_rev_t iter)Frees the memory used by iter.
void bernoulli_fmpq_vec_no_cache(fmpq * res, ulong a, slong num)Sets res to the num consecutive Bernoulli numbers , without reading or writing the cache. To reuse a vector, use bernoulli_cache_compute and bernoulli_fmpq_ui instead. Throws if or .
Cache#
The cache holds 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 of cached values and the array of cached values .
void bernoulli_cache_compute(slong n)Makes sure that 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 is cached, by calling bernoulli_cache_compute(n + 1) if needed.
Bounds#
Returns an integer with , using a table for and otherwise the inequality evaluated in integer arithmetic with a table of logarithms. Returns WORD_MIN if . The bound exceeds the true exponent by at most one percent. Throws if or the computation would overflow; the function is meant for small enough that 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 : by von Staudt–Clausen the denominator is at most , and the estimate is , using for large .
slong bernoulli_global_prec(ulong nmax)Returns the working precision of the iterator for : arith_bernoulli_number_size(nmax) + bernoulli_denom_size(nmax). No guard bits are added, as the denominator estimate is pessimistic.
slong bernoulli_zeta_terms(ulong s, slong prec)Returns the number of terms of the zeta series needed for an error below : about , made odd.
slong bernoulli_power_prec(slong i, ulong s1, slong wp)Returns the precision for the term in a sum computed to absolute precision : , at least 10.
The reverse iterator uses a table for BERNOULLI_REV_MIN (32).
References#
- [Har2010] D. Harvey, "A multimodular algorithm for computing Bernoulli numbers" (2010), Mathematics of Computation 79.272: 2361-2370
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).
@misc{flint-doc-bernoulli-index,
author = {{The FLINT team}},
title = {{bernoulli.h (module overview)}},
howpublished = {FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev},
year = {2026},
url = {https://flintlib.org/doc/bernoulli/index.html},
note = {Generated 2026-11-02}
}