FLINT3.7.0-dev
Algorithm

Bernoulli numbers

Cost
O(n2log⁡O(1)n)O(n^2 \log^{O(1)} n) bit operations for an isolated BnB_n by either the zeta or the multimodular method; O(n3)O(n^3) with a small constant for all of B0,…,BnB_0, \ldots, B_n with the reverse iterator.

The Bernoulli numbers BnB_n are the rational numbers defined by x/(ex−1)=∑n≥0Bnxn/n!x/(e^x - 1) = \sum_{n \ge 0} B_n x^n/n!. They vanish for odd n≥3n \ge 3, alternate in sign for even nn, and grow quickly:

B2m=(−1)m+12 (2m)!(2π)2mζ(2m),log⁡2∣Bn∣=nlog⁡2n2πe+O(log⁡n).B_{2m} = (-1)^{m+1} \frac{2\, (2m)!}{(2\pi)^{2m}} \zeta(2m), \qquad \log_2 |B_n| = n \log_2 \frac{n}{2\pi e} + O(\log n).

The numerator of BnB_n thus has about nlog⁡2nn \log_2 n bits; that of B106B_{10^6} has about 15.8 million. For the classical theory see [IR1990, Chapter 15] and [Coh2007b, Chapter 9].

The denominator#

Theorem 1 (von Staudt–Clausen). For even n≥2n \ge 2,

Bn+∑(p−1)∣n1p∈Z,B_n + \sum_{(p - 1) \mid n} \frac{1}{p} \in \mathbb{Z},

the sum running over the primes pp with (p−1)∣n(p - 1) \mid n. In particular the denominator of BnB_n in lowest terms is ∏(p−1)∣np\prod_{(p-1) \mid n} p, squarefree and divisible by 6.

The denominator is therefore found from the divisors of nn (arith_bernoulli_number_denom tests the primes up to n+1n + 1, with a table for n≤178n \le 178). It is small: at most ∏d∣n(d+1)\prod_{d \mid n} (d + 1), so it has at most σ0(n)log⁡2(n+1)\sigma_0(n) \log_2(n+1) bits, and usually far fewer. All methods below therefore compute the numerator Nn=dnBnN_n = d_n B_n, an integer.

The zeta function method#

Since dnd_n is known, it suffices to compute BnB_n numerically with an absolute error below 1/(2dn)1/(2 d_n), that is, to about log⁡2∣Bn∣+log⁡2dn+1\log_2 |B_n| + \log_2 d_n + 1 significant bits. With pp bits of precision, the formula above needs n!n! and πn\pi^n (cheap: binary splitting and a power) and ζ(n)\zeta(n), which is 1+O(2−n)1 + O(2^{-n}) and for which very few terms are needed:

ζ(n)=∑j=1Jj−n+ϵ,0<ϵ<∫J∞t−n dt<J1−n,\zeta(n) = \sum_{j=1}^{J} j^{-n} + \epsilon, \qquad 0 < \epsilon < \int_J^\infty t^{-n}\, dt < J^{1-n},

so J≈2p/(n−1)J \approx 2^{p/(n-1)} terms suffice, which is O(n/log⁡n)O(n/\log n) for p≈nlog⁡2np \approx n \log_2 n. Equivalently one evaluates the Euler product ζ(n)−1=∏q≤J(1−q−n)\zeta(n)^{-1} = \prod_{q \le J} (1 - q^{-n}) over primes, with O(n/log⁡2n)O(n/\log^2 n) factors. FLINT's arb_bernoulli_ui_zeta uses the Euler product when nn is small compared with the precision and an asymptotic expansion of ζ(n)\zeta(n) otherwise, in ball arithmetic. _bernoulli_fmpq_ui_zeta multiplies the ball by dnd_n and returns the unique integer it contains (increasing the precision in the unlikely event that the ball contains two). Each power q−nq^{-n} costs O(M(nlog⁡n)log⁡n)O(M(n \log n) \log n), so the total is O(nM(nlog⁡n))O(n M(n \log n)), about n2log⁡2+o(1)nn^2 \log^{2+o(1)} n bit operations.

The reverse iterator#

To compute all of B0,B2,…,BnB_0, B_2, \ldots, B_n, evaluating each from scratch would cost n3n^3. Bloemen's observation [Blo2009] is that the power table can be shared: if the powers j−nj^{-n} are stored in fixed point with pp fractional bits, then j−(n−2)=j−n⋅j2j^{-(n-2)} = j^{-n} \cdot j^2 is one multiplication by a word. Going down in nn thus updates the table with cheap exact multiplications (going up would need divisions), and the precision and the number of terms needed decrease along the way.

Algorithm 1: Reverse iteration

Input: an even nmax⁡n_{\max}.

Output: Bnmax⁡,Bnmax⁡−2,…,B0B_{n_{\max}}, B_{n_{\max} - 2}, \ldots, B_0.

  1. Choose the precision pp from the size of Bnmax⁡B_{n_{\max}} and of its denominator, and J≈2(p+1)/(nmax⁡−1)J \approx 2^{(p+1)/(n_{\max} - 1)}.
  2. For odd j=3,5,…,Jj = 3, 5, \ldots, J compute Pj=⌊2pj−nmax⁡⌉P_j = \lfloor 2^p j^{-n_{\max}} \rceil with a bound on the rounding error. Compute F=2 nmax⁡!/(2π)nmax⁡F = 2\, n_{\max}! / (2\pi)^{n_{\max}} as a ball.
  3. For n=nmax⁡,nmax⁡−2,…n = n_{\max}, n_{\max} - 2, \ldots:

    1. Z=1+2−p∑jPjZ = 1 + 2^{-p} \sum_j P_j, plus the accumulated rounding error and the tail J1−nJ^{1-n} as a radius. This encloses ∑j oddj−n=(1−2−n)ζ(n)\sum_{j \text{ odd}} j^{-n} = (1 - 2^{-n}) \zeta(n).
    2. ζ(n)=Z⋅2n/(2n−1)\zeta(n) = Z \cdot 2^n/(2^n - 1); Bn=±Fζ(n)B_n = \pm F \zeta(n); round dnBnd_n B_n to the integer in the ball.
    3. Update Pj←j2PjP_j \gets j^2 P_j, F←F(2π)2/(n(n−1))F \gets F (2\pi)^2 / (n(n-1)).
    4. Every 32 steps, lower pp and JJ to the values needed for the current nn, truncating the PjP_j.

Each step costs O(J)O(J) multiplications of a pp-bit number by a word, O(n/log⁡n⋅nlog⁡n)=O(n2)O(n/\log n \cdot n \log n) = O(n^2) bit operations, so all the even Bernoulli numbers up to nn cost O(n3)O(n^3) in the worst case but with a very small constant, and blocks of a range are independent, which makes the computation easy to parallelise. bernoulli_fmpq_vec_no_cache splits the range into blocks, runs one reverse iterator per block (largest blocks first) on the threads of flint_set_num_threads, and the cache used by the arb special functions is filled this way.

Harvey's multimodular algorithm#

Harvey [Har2010] computes Bn mod pB_n \bmod p for a prime pp in time O(plog⁡1+o(1)p)O(p \log^{1+o(1)} p), independently of nn:

  1. If (p−1)∣n(p - 1) \mid n, pp divides the denominator. Otherwise, by Kummer's congruence Bn/n≡Bm/m(modp)B_n/n \equiv B_m/m \pmod p for m=n mod (p−1)m = n \bmod (p-1), so it suffices to compute Bm/m mod pB_m/m \bmod p with 2≤m≤p−32 \le m \le p - 3.
  2. Voronoi's congruence expresses (cm−1)Bm/m(c^m - 1) B_m / m, for any cc coprime to pp, as a sum over x∈(Z/pZ)×x \in (\mathbb{Z}/p\mathbb{Z})^\times of xm−1x^{m-1} times a simple function of ⌊cx/p⌋\lfloor cx/p \rfloor. Enumerating xx as the powers of a primitive root gg, and using the symmetry x↔p−xx \leftrightarrow p - x, gives a sum of (p−1)/2(p-1)/2 terms, each one multiplication modulo pp.
  3. With c=2c = 2 (when 2m≢1(modp)2^m \not\equiv 1 \pmod p), the floor terms are the bits of the binary expansion of x/px/p. Harvey reorganises the sum so that the inner loop reads the expansion of 1/p1/p word by word and accumulates into tables indexed by 8-bit chunks; the tables are combined with precomputed weights at the end. This makes the constant very small. If 2m≡12^m \equiv 1, the slower generic sum with c=gc = g is used.

With all primes 5≤p≤X5 \le p \le X, (p−1)∤n(p-1) \nmid n, up to X≈nlog⁡nX \approx n \log n, the product exceeds ∣Nn∣|N_n|, and the Chinese remainder theorem gives the numerator: O(n2log⁡O(1)n)O(n^2 \log^{O(1)} n) operations in total, but fully parallel and with smaller constants than the zeta method for large nn. FLINT combines the two methods: the residues modulo a product MM of primes give the low part r=Nn mod Mr = N_n \bmod M, and the zeta method only needs to find the integer t=(Nn−r)/Mt = (N_n - r)/M, a number with a fraction 1−α1 - \alpha of the bits of NnN_n. The default α\alpha grows from 0 at n=18000n = 18000 to 0.280.28 for large nn.

Asymptotically faster methods exist: Harvey's later algorithm computes BnB_n in O(n4/3+o(1))O(n^{4/3 + o(1)}) bit operations [HarveyBern2014], and Brent and Harvey compute all of B0,…,BnB_0, \ldots, B_n in quasi-optimal time with power series [BrentHarvey2013]; they are not implemented.

Short vectors#

For a vector B0,…,Bn−1B_0, \ldots, B_{n-1} with moderate nn, arith uses:

  • Ramanujan's recursion (_arith_bernoulli_number_vec_recursive, n<700n < 700). Ramanujan's identity [Ram1911]

    (m+3m)Bm={m+33−m+36−∑j=1⌊m/6⌋(m+3m−6j)Bm−6j,m≡0,2 or 4(mod6),\binom{m+3}{m} B_m = \begin{cases} \frac{m+3}{3} \\ -\frac{m+3}{6} \end{cases} - \sum_{j=1}^{\lfloor m/6 \rfloor} \binom{m+3}{m - 6j} B_{m-6j}, \quad m \equiv 0, 2 \text{ or } 4 \pmod 6,

    (with (m+3)/3(m+3)/3 for m≡0,2m \equiv 0, 2 and −(m+3)/6-(m+3)/6 for m≡4m \equiv 4) needs only a sixth of the previous values. FLINT runs it on integers over the common denominator (n+1)#(n + 1)\#, so that no GCD is needed until the final division by the true denominators.

  • Power series modulo primes (_arith_bernoulli_number_vec_multi_mod, n≥700n \ge 700). The even Bernoulli numbers are the coefficients of

    x2cosh⁡x−1=∑k≥0(2−4k) B2k(2k)!x2k,\frac{x^2}{\cosh x - 1} = \sum_{k \ge 0} \frac{(2 - 4k)\, B_{2k}}{(2k)!} x^{2k},

    obtained by one power series inversion of (cosh⁡x−1)/x2(\cosh x - 1)/x^2, which has only the even terms [BuhlerCrandallSompolski1992]. Multiplying by the known denominators and factorials modulo each prime gives the numerators modulo the prime, and Chinese remaindering the numerators.

Euler numbers#

The Euler numbers EnE_n, 1/cosh⁡t=∑Entn/n!1/\cosh t = \sum E_n t^n/n!, are integers with ∣E2m∣=22m+2(2m)! β(2m+1)/π2m+1|E_{2m}| = 2^{2m+2} (2m)!\, \beta(2m+1)/\pi^{2m+1}, where β(s)=∑k≥0(−1)k(2k+1)−s\beta(s) = \sum_{k \ge 0} (-1)^k (2k+1)^{-s} is the Dirichlet beta function. FLINT computes an isolated EnE_n from this formula for n<2000n < 2000 and with a multimodular method combined with the beta function above (arb_fmpz_euler_number_ui), and a vector by inverting the power series of cosh⁡\cosh modulo primes (arith_euler_number_vec).

Use in FLINT#

FunctionMethod
bernoulli_fmpq_uithe cache if possible; zeta for n<18000n < 18000; zeta with multimodular low part above
_bernoulli_fmpq_ui_zetathe zeta function via arb_bernoulli_ui_zeta
_bernoulli_fmpq_ui_multi_modbernoulli_mod_p_harvey on many primes in parallel, plus the zeta function for the high part
bernoulli_mod_p_harveyHarvey's algorithm with Kummer's congruence
bernoulli_rev_init, bernoulli_rev_nextthe reverse iterator Algorithm 1
bernoulli_fmpq_vec_no_cache, bernoulli_cache_computereverse iterators on blocks, in parallel
arith_bernoulli_number_vecRamanujan's recursion (n<700n < 700) or power series modulo primes
arith_euler_number, arith_euler_number_vecthe beta function and multimodular methods; power series modulo primes

The generic functions call these: gr_bernoulli_ui uses bernoulli_fmpq_ui over Q\mathbb{Q} (and in rings without a real precision, followed by a conversion), gr_bernoulli_vec uses bernoulli_fmpq_vec_no_cache over Q\mathbb{Q}, and gr_eulernum_vec uses arith_euler_number_vec over Z\mathbb{Z}; in real and complex rings they compute numerically with arb.

Referenced from#

References#

  • [IR1990] K. Ireland and M. Rosen, A Classical Introduction to Modern Number Theory, second edition, Graduate Texts in Mathematics 84, Springer, 1990. https://doi.org/10.1007/978-1-4757-2103-4
  • [Coh2007b] H. Cohen, Number Theory, Volume II: Analytic and Modern Tools, Graduate Texts in Mathematics 240, Springer, 2007. https://doi.org/10.1007/978-0-387-49894-2
  • [Blo2009] R. Bloemen, "Even faster zeta(2n) calculation!", https://web.archive.org/web/20141101133659/http://xn--2-umb.com/09/11/even-faster-zeta-calculation
  • [Har2010] D. Harvey, "A multimodular algorithm for computing Bernoulli numbers" (2010), Mathematics of Computation 79.272: 2361-2370
  • [HarveyBern2014] D. Harvey, "A subquadratic algorithm for computing the n-th Bernoulli number", Mathematics of Computation 83 (2014), 2471–2477. https://doi.org/10.1090/S0025-5718-2014-02832-X
  • [BrentHarvey2013] R. P. Brent and D. Harvey, "Fast computation of Bernoulli, tangent and secant numbers", in Computational and Analytical Mathematics, Springer Proceedings in Mathematics & Statistics 50 (2013), 127–142. https://doi.org/10.1007/978-1-4614-7621-4_8
  • [Ram1911] S. Ramanujan, "Some properties of Bernoulli's numbers", Journal of the Indian Mathematical Society 3 (1911), 219–234.
  • [BuhlerCrandallSompolski1992] Buhler, J.P. and Crandall, R.E. and Sompolski, R.W. : Irregular primes to one million : Math. Comp. 59:2000 (1992) 717–722
Cite this page

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