Key words

Mathematics Subject Classifications (2010)

8.1 Introduction

The Bernoulli numbers are rational numbers B n defined by the generating function

$$\displaystyle{ \sum _{n\geq 0}B_{n}\,\frac{{z}^{n}} {n!} = \frac{z} {\exp (z) - 1}\,. }$$
(8.1)

Bernoulli numbers are of interest in number theory and are related to special values of the Riemann zeta function (see Sect. 8.2). They also occur as coefficients in the Euler–Maclaurin formula, so are relevant to high-precision computation of special functions [7, Sect. 4.5].

It is sometimes convenient to consider scaled Bernoulli numbers

$$\displaystyle{ C_{n} = \frac{B_{2n}} {(2n)!}\,, }$$
(8.2)

with generating function

$$\displaystyle{ \sum _{n\geq 0}C_{n}\,{z}^{2n} = \frac{z/2} {\tanh (z/2)}\,. }$$
(8.3)

The generating functions (8.1) and (8.3) only differ by the single term B 1 z, since the other odd terms vanish.

The Tangent numbers T n and Secant numbers S n are defined by

$$\displaystyle{ \sum _{n>0}T_{n}\, \frac{{z}^{2n-1}} {(2n - 1)!} =\tan z,\;\;\;\;\sum _{n\geq 0}S_{n}\, \frac{{z}^{2n}} {(2n)!} =\sec z\,. }$$
(8.4)

In this paper, which is based on an a talk given by the first author at a workshop held to mark Jonathan Borwein’s sixtieth birthday, we consider some algorithms for computing Bernoulli, Tangent, and Secant numbers. For background, combinatorial interpretations, and references, see Abramowitz and Stegun [1, Chap. 23] (where the notation differs from ours, e.g. ( − 1)n E 2n is used for our S n ), and Sloane’s [27] sequences A000367, A000182, and A000364.

Let M(n) be the number of bit-operations required for n-bit integer multiplication. The Schönhage–Strassen algorithm [25] gives M(n) = O(nlognloglogn), and Fürer [17] has recently given an improved bound \(M(n) = O(n(\log n){2}^{{\log }^{{\ast}}n })\). For simplicity we merely assume that \(M(n) = O(n{(\log n)}^{1+o(1)})\), where the o(1) term depends on the precise algorithm used for multiplication. For example, if the Schönhage–Strassen algorithm is used, then the o(1) term can be replaced by logloglogn ∕ loglogn.

In Sects. 8.2 and 8.3 we mention some relevant and generally well-known facts concerning Bernoulli, Tangent, and Secant numbers.

Recently, Harvey [20] showed that the single number B n can be computed in O(n 2(logn)2 + o(1)) bit-operations using a modular algorithm. In this paper we show that all the Bernoulli numbers B 0, , B n can be computed with the same complexity bound (and similarly for Secant and Tangent numbers).

In Sect. 8.4 we give a relatively simple algorithm that achieves the slightly weaker bound O(n 2(logn)3 + o(1)). In Sect. 8.5 we describe the improvement to O(n 2(logn)2 + o(1)). The idea is similar to that espoused by Steel [29], although we reduce the problem to division rather than multiplication. It is an open question whether the single number B 2n can be computed in o(n 2) bit-operations.

In Sect. 8.6 we give very short in-place algorithms for computing the first n Secant or Tangent numbers using O(n 2) integer operations. These algorithms are extremely simple and fast for moderate values of n (say n ≤ 1000), although asymptotically not as fast as the algorithms given in Sects. 8.4 and 8.5. Bernoulli numbers can easily be deduced from the corresponding Tangent numbers using the relation (8.14) below.

8.2 Bernoulli Numbers

From the generating function (8.1) it is easy to see that the B n are rational numbers, with \(B_{2n+1} = 0\) if n > 0. The first few nonzero B n are B 0 = 1, \(B_{1} = -1/2\), \(B_{2} = 1/6\), \(B_{4} = -1/30\), \(B_{6} = 1/42\), \(B_{8} = -1/30\), \(B_{10} = 5/66\), \(B_{12} = -691/2730\), and \(B_{14} = 7/6\).

The denominators of the Bernoulli numbers are given by the Von Staudt–Clausen Theorem [12, 28], which states that

$$\displaystyle{ B^{\prime}_{2n}:= B_{2n}\; +\sum _{(p-1)\vert 2n}\frac{1} {p}\; \in \mathbb{Z}\,. }$$

Here the sum is over all primes p for which p − 1 divides 2n.

Since the “correction” \(B^{\prime}_{2n} - B_{2n}\) is easy to compute, it might be convenient in a program to store the integers B′ 2n instead of the rational numbers B 2n or C n .

Euler found that the Riemann zeta-function for even non-negative integer arguments can be expressed in terms of Bernoulli numbers—the relation is

$$\displaystyle{ {(-1)}^{n-1} \frac{B_{2n}} {(2n)!} = \frac{2\zeta (2n)} {{(2\pi )}^{2n}} \,. }$$
(8.5)

Since \(\zeta (2n) = 1 + O({4}^{-n})\) as n → + , we see that

$$\displaystyle{ \vert B_{2n}\vert \sim \frac{2\,(2n)!} {{(2\pi )}^{2n}} \,. }$$

From Stirling’s approximation to (2n)!, the number of bits in the integer part of B 2n is 2nlgn + O(n) (we write lg for log2). Thus, it takes Ω(n 2 logn) space to store B 1, , B n . We cannot expect any algorithm to compute B 1, , B n in fewer than Ω(n 2 logn) bit-operations.

Another connection between the Bernoulli numbers and the Riemann zeta-function is the identity

$$\displaystyle{ \frac{B_{n+1}} {n + 1} = -\zeta (-n) }$$
(8.6)

for \(n \in \mathbb{Z}\), n ≥ 1. This follows from (8.5) and the functional equation for the zeta-function or directly from a contour integral representation of the zeta-function [31].

From the generating function (8.1), multiplying both sides by exp(z) − 1 and equating coefficients of z, we obtain the recurrence

$$\displaystyle{ \sum _{j=0}^{k}\binom{k + 1}{j}\,B_{ j} = 0\;\text{for}\;k > 0. }$$
(8.7)

This recurrence has traditionally been used to compute B 0, , B 2n with O(n 2) arithmetic operations, for example, in [22]. However, this is unsatisfactory if floating-point numbers are used, because the recurrence is numerically unstable: the relative error in the computed B 2n is of order \({4}^{n}\varepsilon\) if the floating-point arithmetic has precision \(\varepsilon\), i.e., \(\lg (1/\varepsilon )\) bits.

Let C n be defined by (8.2). Then, multiplying each side of (8.3) by \(\sinh (z/2)/\) (z ∕ 2) and equating coefficients gives the recurrence

$$\displaystyle{ \sum _{j=0}^{k} \frac{C_{j}} {(2k + 1 - 2j)!\,{4}^{k-j}} = \frac{1} {(2k)!\;{4}^{k}}\,. }$$
(8.8)

Using this recurrence to evaluate \(C_{0},C_{1},\ldots,C_{n}\), the relative error in the computed C n is only \(O({n}^{2}\varepsilon )\), which is satisfactory from a numerical point of view.

Equation (8.5) can be used in several ways to compute Bernoulli numbers. If we want just one Bernoulli number B 2n then ζ (2n) on the right-hand side of (8.5) can be evaluated to sufficient accuracy using the Euler product: this is the “zeta-function” algorithm for computing Bernoulli numbers mentioned (with several references to earlier work) by Harvey [20]. On the other hand, if we want several Bernoulli numbers, then we can use the generating function

$$\displaystyle{ \frac{\pi z} {\tanh (\pi z)} = -2\sum _{k=0}^{\infty }{(-1)}^{k}\zeta (2k){z}^{2k}\,, }$$
(8.9)

computing the coefficients of z 2k, kn, to sufficient accuracy, as mentioned in [3, 8, 9]. This is similar to the fast algorithm that we describe in Sect. 8.4. The similarity can be seen more clearly if we replace πz by z in (8.9), giving

$$\displaystyle{ \frac{z} {\tanh (z)} = -2\sum _{k=0}^{\infty }{(-1)}^{k}{\frac{\zeta (2k)} {\pi }^{2k}} {z}^{2k}\,, }$$
(8.10)

since it is the rational number ζ (2n) ∕ π 2n that we need in order to compute B 2n from (8.5). In fact, it is easy to see that (8.10) is equivalent to (8.3).

There is a vast literature on Bernoulli, Tangent, and Secant numbers. For example, the bibliography of Dilcher and Slavutskii [15] contains more than 2, 000 items. Thus, we do not attempt to give a complete list of references to related work. However, we briefly mention the problem of computing irregular primes [10, 11], which are odd primes p such that p divides the class number of the pth cyclotomic field. The algorithms that we present in Sects. 8.4 and 8.5 below are not suitable for this task because they take too much memory. It is much more space-efficient to use a modular algorithm where the computations are performed modulo a single prime (or maybe the product of a small number of primes), as in [10, 11, 14, 20]. Space can also be saved by the technique of “multisectioning”, which is described by Crandall [13, Sect. 3.2] and Hare [19].

8.3 Tangent and Secant Numbers

The Tangent numbers T n   (n > 0) (also called zag numbers) are defined by

$$\displaystyle{ \sum _{n>0}T_{n}\, \frac{{z}^{2n-1}} {(2n - 1)!} =\tan z = \frac{\sin z} {\cos z}\,. }$$

Similarly, the Secant numbers S n   (n ≥ 0) (also called Euler or zig numbers) are defined by

$$\displaystyle{ \sum _{n\geq 0}S_{n}\, \frac{{z}^{2n}} {(2n)!} =\sec z = \frac{1} {\cos z}. }$$

Unlike the Bernoulli numbers, the Tangent and Secant numbers are positive integers. Because tan z and sec z have poles at \(z =\pi /2\), we expect T n to grow roughly like \((2n - 1)!{(2/\pi )}^{n}\) and S n like (2n)! (2 ∕ π)n. To obtain more precise estimates, let

$$\displaystyle{ \zeta _{0}(s) = (1 - {2}^{-s})\zeta (s) = 1 + {3}^{-s} + {5}^{-s} + \cdots }$$

be the odd zeta-function. Then

$$\displaystyle{ \frac{T_{n}} {(2n - 1)!} ={ \frac{{2}^{2n+1}\zeta _{0}(2n)} {\pi }^{2n}} \sim { \frac{{2}^{2n+1}} {\pi }^{2n}} }$$
(8.11)

(this can be proved in the same way as Euler’s relation (8.5) for the Bernoulli numbers). We also have [1, (23.2.22)]

$$\displaystyle{ \frac{S_{n}} {(2n)!} ={ \frac{{2}^{2n+2}\beta (2n + 1)} {\pi }^{2n+1}} \sim { \frac{{2}^{2n+2}} {\pi }^{2n+1}} \,, }$$
(8.12)

where

$$\displaystyle{ \beta (s) =\sum _{ j=0}^{\infty }{(-1)}^{j}{(2j + 1)}^{-s}. }$$
(8.13)

From (8.5) and (8.11), we see that

$$\displaystyle{ T_{n} = {(-1)}^{n-1}{2}^{2n}({2}^{2n} - 1)\frac{B_{2n}} {2n} \,. }$$
(8.14)

This can also be proved directly, without involving the zeta-function, by using the identity

$$\displaystyle{ \tan z = \frac{1} {\tan z} - \frac{2} {\tan (2z)}\,. }$$

Since \(T_{n} \in \mathbb{Z}\), it follows from (8.14) that the odd primes in the denominator of B 2n must divide 22n − 1. This is compatible with the Von Staudt–Clausen theorem, since (p − 1) | 2n implies p | (22n − 1) by Fermat’s little theorem.

T n has about 4n more bits than \(\lceil B_{2n}\rceil \), but both have 2nlgn + O(n) bits, so asymptotically there is not much difference between the sizes of T n and \(\lceil B_{2n}\rceil \). Thus, if our aim is to compute B 2n , we do not lose much by first computing T n , and this may be more convenient since \(T_{n} \in \mathbb{Z}\), \(B_{2n} \in \mathbb{Q}\).

8.4 A Fast Algorithm for Bernoulli Numbers

Harvey [20] showed how B n could be computed exactly, using a modular algorithm and the Chinese remainder theorem, in O(n 2(logn)2 + o(1)) bit-operations. The same complexity can be obtained using (8.5) and the Euler product for the zeta-function (see the discussion in Harvey [20, Sect. 1]).

In this section we show how to compute all of B 0, , B n with almost the same complexity bound (only larger by a factor O(logn)). In Sect. 8.5 we give an even faster algorithm, which avoids the O(logn) factor.

Let \(A(z) = a_{0} + a_{1}z + a_{2}{z}^{2} + \cdots \) be a power series with coefficients in \(\mathbb{R}\), with a 0 ≠ 0. Let \(B(z) = b_{0} + b_{1}z + \cdots \) be the reciprocal power series, so A(z)B(z) = 1. Using the FFT, we can multiply polynomials of degree n − 1 with O(nlogn) real operations. Using Newton’s method [24, 26], we can compute b 0, , b n − 1 with the same complexity O(nlogn), up to a constant factor.

Taking \(A(z) = (\exp (z) - 1)/z\) and working with N-bit floating-point numbers, where \(N = n\lg (n) + O(n)\), we get B 0, , B n to sufficient accuracy to deduce the exact (rational) result. (Alternatively, use (8.3) to avoid computing the terms with odd subscripts, since these vanish except for B 1.) The work involved is O(nlogn) floating-point operations, each of which can be done with N-bit accuracy in O(n(logn)2 + o(1)) bit-operations. Thus, overall we get B 0, , B n with O(n 2(logn)3 + o(1)) bit-operations. Similarly for Secant and Tangent numbers. We omit a precise specification of N and a detailed error analysis of the algorithm, since it is improved in the following section.

8.5 A Faster Algorithm for Tangent and Bernoulli Numbers

To improve the algorithm of Sect. 8.4 for Bernoulli numbers, we use the “Kronecker–Schönhage trick” [7, Sect. 1.9]. Instead of working with power series A(z) (or polynomials, which can be regarded as truncated power series), we work with binary numbers A(z) where z is a suitable (negative) power of 2.

The idea is to compute a single real number \(\mathcal{A}\) which is defined in such a way that the numbers that we want to compute are encoded in the binary representation of \(\mathcal{A}\). For example, consider the series

$$\displaystyle{\sum _{k>0}{k}^{2}{z}^{k} = \frac{z(1 + z)} {{(1 - z)}^{3}},\;\;\vert z\vert < 1.}$$

The right-hand side is an easily computed rational function of z, say A(z). We use decimal rather than binary for expository purposes. With \(z = 1{0}^{-3}\) we easily find

$$\displaystyle{A(1{0}^{-3}) = \frac{1001000} {997002999} = 0.00\underline{1}\,00\underline{4}\,00\underline{9}\,0\underline{16}\,0\underline{25}\,0\underline{36}\,0\underline{49}\,0\underline{64}\,0\underline{81}\,\underline{100}\,\cdots }$$

Thus, if we are interested in the finite sequence of squares \(({1}^{2},{2}^{2},{3}^{2},\ldots,1{0}^{2})\), it is sufficient to compute \(\mathcal{A} = A(1{0}^{-3})\) correctly rounded to 30 decimal places, and we can then “read off” the squares from the decimal representation of \(\mathcal{A}\).

Of course, this example is purely for illustrative purposes, because it is easy to compute the sequence of squares directly. However, we use the same idea to compute Tangent numbers. Suppose we want the first n Tangent numbers \((T_{1},T_{2},\ldots,T_{n})\). The generating function

$$\displaystyle{\tan z =\sum _{k\geq 1}T_{k}\, \frac{{z}^{2k-1}} {(2k - 1)!}}$$

gives us almost what we need, but not quite, because the coefficients are rationals, not integers. Instead, consider

$$\displaystyle{ (2n - 1)!\tan z =\sum _{ k=1}^{n}T^{\prime}_{ k,n}\,{z}^{2k-1} + R_{ n}(z), }$$
(8.15)

where

$$\displaystyle{ T^{\prime}_{k,n} = \frac{(2n - 1)!} {(2k - 1)!}\,T_{k} }$$
(8.16)

is an integer for 1 ≤ kn, and

$$\displaystyle{ R_{n}(z) =\sum _{ k=n+1}^{\infty }T^{\prime}_{ k,n}\,{z}^{2k-1} = (2n - 1)!\sum _{ k=n+1}^{\infty }T_{ k}\, \frac{{z}^{2k-1}} {(2k - 1)!} }$$
(8.17)

is a remainder term which is small if z is sufficiently small. Thus, choosing \(z = {2}^{-p}\) with p sufficiently large, the first 2np binary places of (2n − 1)! tan z define \(T^{\prime}_{1,n},T^{\prime}_{2,n},\ldots,T^{\prime}_{n,n}\). Once we have computed \(T^{\prime}_{1,n},T^{\prime}_{2,n},\ldots,T^{\prime}_{n,n}\) it is easy to deduce \(T_{1},T_{2},\ldots,T_{n}\) from

$$\displaystyle{T_{k} = \frac{T^{\prime}_{k,n}} {(2n - 1)!/(2k - 1)!}\,.}$$

For this idea to work, two conditions must be satisfied. First, we need

$$\displaystyle{ 0 \leq T^{\prime}_{k,n} < 1/{z}^{2} = {2}^{2p},\;\;1 \leq k \leq n, }$$
(8.18)

so we can read off the T′ k,n from the binary representation of (2n − 1)! tan z. Since we have a good asymptotic estimate for T k , it is not hard to choose p sufficiently large for this condition to hold.

Second, we need the remainder term R n (z) to be sufficiently small that it does not influence the estimation of T′ n,n . A sufficient condition is

$$\displaystyle{ 0 \leq R_{n}(z) < {z}^{2n-1}. }$$
(8.19)

Choosing z sufficiently small (i.e., p sufficiently large) guarantees that condition (8.19) holds, since R n (z) is O(z 2n + 1) as z → 0 with n fixed.

Lemmas 8.3 and 8.4 below give sufficient conditions for (8.18) and (8.19) to hold.

Lemma 8.1.

$$\displaystyle{ \frac{T_{k}} {(2k - 1)!} \leq {\left (\frac{2} {\pi } \right )}^{2(k-1)}\;\;\mathrm{for}\;\;k \geq 1.}$$

Proof.

From (8.11),

$$\displaystyle{ \frac{T_{k}} {(2k - 1)!} = 2{\left (\frac{2} {\pi } \right )}^{2k}\zeta _{ 0}(2k) \leq 2{\left (\frac{2} {\pi } \right )}^{2k}\zeta _{ 0}(2) \leq {\left (\frac{2} {\pi } \right ){}}^{2k}\frac{{\pi }^{2}} {4} ={ \left (\frac{2} {\pi } \right )}^{2(k-1)}.}$$

Lemma 8.2.

\((2n - 1)! \leq {n}^{2n-1}\) for n ≥ 1.

Proof.

$$\displaystyle{(2n - 1)! = n\;\prod _{j=1}^{n-1}(n - j)(n + j) = n\;\prod _{ j=1}^{n-1}({n}^{2} - {j}^{2}) \leq {n}^{2n-1}}$$

with equality iff n = 1.

Lemma 8.3.

If k ≥ 1, n ≥ 2, \(p = \lceil n\lg (n)\rceil \), \(z = {2}^{-p}\), and T′ k,n is as in (8.16), then z ≤ n −n and T′ k,n < 1∕z 2 .

Proof.

We have \(z = {2}^{-p} = {2}^{-\lceil n\lg (n)\rceil }\leq {2}^{-n\lg (n)} = {n}^{-n}\), which proves the first part of the Lemma.

Assume k ≥ 1 and n ≥ 2. From Lemma 8.1, we have

$$\displaystyle{T^{\prime}_{k,n} \leq (2n - 1)!{\left (\frac{2} {\pi } \right )}^{2(k-1)} \leq (2n - 1)!,}$$

and from Lemma 8.2 it follows that

$$\displaystyle{T^{\prime}_{k,n} \leq {n}^{2n-1} < {n}^{2n}.}$$

From the first part of the Lemma, n 2n ≤ 1 ∕ z 2, so the second part follows.

Lemma 8.4.

If n ≥ 2, \(p = \lceil n\lg (n)\rceil \), \(z = {2}^{-p}\), and R n (z) is as defined in (8.17), then \(0 < R_{n}(z) < 0.1\,{z}^{2n-1}\) .

Proof.

Since all the terms in the sum defining R n (z) are positive, it is immediate that R n (z) > 0. Since n ≥ 2, we have p ≥ 2 and z ≤ 1 ∕ 4. Now, using Lemma 8.1,

$$\displaystyle\begin{array}{rcl} R_{n}(z)& =& \sum _{k=n+1}^{\infty }T^{\prime}_{ k,n}{z}^{2k-1} {}\\ & \leq & (2n - 1)!\,\sum _{k=n+1}^{\infty }{\left (\frac{2} {\pi } \right )}^{2(k-1)}{z}^{2k-1} {}\\ & \leq & (2n - 1)!\,{\left (\frac{2} {\pi } \right )}^{2n}{z}^{2n+1}\left (1 +{ \left (\frac{2z} {\pi } \right )}^{2} +{ \left (\frac{2z} {\pi } \right )}^{4} + \cdots \,\right ) {}\\ & \leq & (2n - 1)!\,{\left (\frac{2} {\pi } \right )}^{2n}{z}^{2n+1}\left /\left (1 -{\left (\frac{2z} {\pi } \right )}^{2}\right )\right.\,. {}\\ \end{array}$$

Since z ≤ 1 ∕ 4, we have \(1/(1 - {(2z/\pi )}^{2}) < 1.026\). Also, from Lemma 8.2, \((2n - 1)! \leq {n}^{2n-1}\). Thus, we have

$$\displaystyle{ \frac{R_{n}(z)} {{z}^{2n-1}} < 1.026\,{n}^{2n-1}{\left (\frac{2} {\pi } \right )}^{2n}{z}^{2}. }$$

Now z 2n − 2n from the first part of Lemma 8.3, so

$$\displaystyle{ \frac{R_{n}(z)} {{z}^{2n-1}} < \frac{1.026} {n}{ \left (\frac{2} {\pi } \right )}^{2n}. }$$
(8.20)

The right-hand side is a monotonic decreasing function of n, so is bounded above by its value when n = 2, giving \(R_{n}(z)/{z}^{2n-1} < 0.1\,\).

A high-level description of the resulting Algorithm FastTangentNumbers is given in Fig. 8.1. The algorithm computes the Tangent numbers \(T_{1},T_{2},\ldots,T_{n}\) using the Kronecker–Schönhage trick as described above, and deduces the Bernoulli numbers \(B_{2},B_{4},\ldots,B_{2n}\) from the relation (8.14).

Fig. 8.1
figure 1

Algorithm FastTangentNumbers (also optionally computes Bernoulli numbers)

In order to achieve the best complexity, the algorithm must be implemented carefully using binary arithmetic. The computations of S (an approximation to (2n)!sinz) and C (an approximation to (2n)!cosz) involve computing ratios of factorials such as (2n)! ∕ (2k)!, where 0 ≤ kn. This can be done in time O(n 2(logn)2) by a straightforward algorithm. The N-bit division to compute SC (an approximation to tanz) can be done in time O(N log(N)loglog(N)) by the Schönhage–Strassen algorithm combined with Newton’s method [7, Sect. 4.2.2]. Here it is sufficient to take \(N = 2np + 2 = 2{n}^{2}\lg (n) + O(n)\). Note that

$$\displaystyle{ V =\sum _{ k=1}^{n}{2}^{2(n-k)p}T^{\prime}_{ k,n} }$$
(8.21)

is just the finite sum in (8.15) scaled by z 1 − 2n (a power of two), and the integers T′ k,n can simply be “read off” from the binary representation of V in n blocks of 2p consecutive bits. The T′ k,n can then be scaled by ratios of factorials in time O(n 2(logn)2 + o(1)) to give the Tangent numbers \(T_{1},T_{2},\ldots,T_{n}\).

The correctness of the computed Tangent numbers follows from Lemmas 8.3 and 8.4, apart from possible errors introduced by SC being only an approximation to tan(z). Lemma 8.5 shows that this error is sufficiently small.

Lemma 8.5.

Suppose that n ≥ 2, z, S and C as in Algorithm FastTangentNumbers. Then

$$\displaystyle\begin{array}{rcl}{ z}^{1-2n}(2n - 1)!\,\left \vert \frac{S} {C} -\tan z\right \vert & <& 0.02\,.{}\end{array}$$
(8.22)

Proof.

We use the inequality

$$\displaystyle{ \left \vert \frac{A} {B} -\frac{A^{\prime}} {B^{\prime}}\right \vert \leq \frac{\vert A\vert \cdot \vert B - B^{\prime}\vert + \vert B\vert \cdot \vert A - A^{\prime}\vert } {\vert B\vert \cdot \vert B^{\prime}\vert } \,. }$$
(8.23)

Take A = sinz, B = cosz, \(A^{\prime} = S/(2n)!\), \(B^{\prime} = C/(2n)!\) in (8.23). Since n ≥ 2 we have 0 < z ≤ 1 ∕ 4. Then | A | = | sinz | < z. Also, \(\vert B\vert = \vert \cos z\vert > 31/32\) from the Taylor series \(\cos z = 1 - {z}^{2}/2 + \cdots \), which has terms of alternating sign and decreasing magnitude. By similar arguments, | B′ | ≥ 31 ∕ 32, \(\vert B - B^{\prime}\vert < {z}^{2n}/(2n)!\), and\(\vert A - A^{\prime}\vert < {z}^{2n+1}/(2n + 1)!\). Combining these inequalities and using (8.23), we obtain

$$\displaystyle{\left \vert \frac{S} {C} -\tan z\right \vert < \frac{6 \cdot 32 \cdot 32} {5 \cdot 31 \cdot 31} \frac{{z}^{2n+1}} {(2n)!} < \frac{1.28\,{z}^{2n+1}} {(2n)!} \,.}$$

Multiplying both sides by \({z}^{1-2n}(2n - 1)!\) and using 1. 28 z 2 ∕ (2n) ≤ 0. 02, we obtain the inequality (8.22). This completes the proof of Lemma 8.5.

In view of the constant 0. 02 in (8.22) and the constant 0. 1 in Lemma 8.4, the effect of all sources of error in computing \({z}^{1-2n}(2n - 1)!\tan z\) is at most 0. 12 < 1 ∕ 2, which is too small to change the computed integer V, that is to say, the computed V is indeed given by (8.21).

The computation of the Bernoulli numbers \(B_{2},B_{4},\ldots,B_{2n}\) from T 1, , T n , is straightforward (details depending on exactly how rational numbers are to be represented). The entire computation takes time

$$\displaystyle{ O(N{(\log N)}^{1+o(1)}) = O({n}^{2}{(\log n)}^{2+o(1)}). }$$

Thus, we have proved:

Theorem 8.6.

The Tangent numbers T 1, …,T n and Bernoulli numbers B 2,B 4, …,B 2n can be computed in O(n 2 (logn)2+o(1)) bit-operations using O(n 2 logn) space.

A small modification of the above can be used to compute the Secant numbers \(S_{0},S_{1},\ldots,S_{n}\) in O(n 2(logn)2 + o(1)) bit-operations and O(n 2 logn) space. The bound on Tangent numbers given by Lemma 8.1 can be replaced by the bound

$$\displaystyle{ \frac{S_{n}} {(2n)!} \leq 2{\left (\frac{2} {\pi } \right )}^{2n+1}}$$

which follows from (8.12) since β(2n + 1) < 1.

We remark that an efficient implementation of Algorithm FastTangentNumbers in a high-level language such as Sage [30] or Magma [5] is nontrivial, because it requires access to the internal binary representation of high-precision integers. Everything can be done using (implicitly scaled) integer arithmetic—there is no need for floating-point—but for the sake of clarity we did not include the scaling in Fig. 8.1. If floating-point arithmetic is used, a precision of N bits is sufficient, where \(N = 2np + 2\).

Comparing our Algorithm FastTangentNumbers with Harvey’s modular algorithm [20], we see that there is a space-time trade-off: Harvey’s algorithm uses less space (by a factor of order n) to compute a single B n , but more time (again by a factor of order n) to compute all of B 1, , B n . Harvey’s algorithm has better locality and is readily parallelizable.

In the following section we give much simpler algorithms which are fast enough for most practical purposes and are based on three-term recurrence relations.

8.6 Algorithms Based on Three-Term Recurrences

Akiyama and Tanigawa [21] gave an algorithm for computing Bernoulli numbers based on a three-term recurrence. However, it is only useful for exact computations, since it is numerically unstable if applied using floating-point arithmetic. It is faster to use a stable recurrence for computing Tangent numbers and then deduce the Bernoulli numbers from (8.14).

8.6.1 Bernoulli and Tangent Numbers

We now give a stable three-term recurrence and corresponding in-place algorithm for computing Tangent numbers. The algorithm is perfectly stable since all operations are on positive integers and there is no cancellation. Also, it involves less arithmetic than the Akiyama–Tanigawa algorithm. This is partly because the operations are on integers rather than rationals and partly because there are fewer operations since we take advantage of zeros.

Bernoulli numbers can be computed using Algorithm TangentNumbers and the relation (8.14). The time required for the application of (8.14) is negligible.

The recurrence (8.24) that we use was given by Buckholtz and Knuth [23], but they did not give our in-place Algorithm TangentNumbers explicitly. Related recurrences with applications to parallel computation were considered by Hare [19].

Write t = tan x, \(D =\mathrm{ d}/\mathrm{d}x\), so \(Dt = 1 + {t}^{2}\) and \(D({t}^{n}) = n{t}^{n-1}(1 + {t}^{2})\) for n ≥ 1. It is clear that D n t is a polynomial in t, say P n (t). Write \(P_{n}(t) =\sum _{j\geq 0}p_{n,j}{t}^{j}\). Then \(\mathrm{deg}(P_{n}) = n + 1\) and, from the formula for D(t n),

$$\displaystyle{ p_{n,j} = (j - 1)p_{n-1,j-1} + (j + 1)p_{n-1,j+1}. }$$
(8.24)

We are interested in \(T_{k} = {(\mathrm{d}/\mathrm{d}x)}^{2k-1}\tan x\,\vert _{x=0} = P_{2k-1}(0) = p_{2k-1,0}\), which can be computed from the recurrence (8.24) in O(k 2) operations using the obvious boundary conditions. We save work by noticing that p n, j = 0 if n + j is even. The resulting algorithm is given in Fig. 8.2.

Fig. 8.2
figure 2

Algorithm TangentNumbers

The first for loop initializes \(T_{k} = p_{k-1,k} = (k - 1)!\). The variable T k is then used to store p k, k − 1, \(p_{k+1,k-2}\), , p 2k − 2, 1, p 2k − 1, 0 at successive iterations of the second for loop. Thus, when the algorithm terminates, \(T_{k} = p_{2k-1,0}\), as expected.

The process in the case n = 3 is illustrated in Fig. 8.3, where T k (m) denotes the value of the variable T k at successive iterations m = 1, 2, , n. It is instructive to compare a similar figure for the Akiyama–Tanigawa algorithm in [21].

Fig. 8.3
figure 3

Dataflow in algorithm TangentNumbers for n = 3

Algorithm TangentNumbers takes Θ(n 2) operations on positive integers. The integers T n have O(nlogn) bits, other integers have O(logn) bits. Thus, the overall complexity is O(n 3(logn)1 + o(1)) bit-operations, or O(n 3logn) word-operations if n fits in a single word.

The algorithm is not optimal, but it is good in practice for moderate values of n, and much simpler than asymptotically faster algorithms such as those described in Sects. 8.4 and 8.5. For example, using a straightforward Magma implementation of Algorithm TangentNumbers, we computed the first 1, 000 Tangent numbers in 1.50 s on a 2.26 GHz Intel Core 2 Duo. For comparison, it takes 1.92 s for a single N-bit division computing T in Algorithm FastTangentNumbers (where N = 19, 931, 568 corresponds to n = 1, 000). Thus, we expect the crossover point where Algorithm FastTangentNumbers actually becomes faster to be slightly larger than n = 1, 000 (but dependent on implementation details).

8.6.2 Secant Numbers

A similar algorithm may be used to compute Secant numbers. Let s = sec x, t = tan x, and \(D =\mathrm{ d}/\mathrm{d}x\). Then Ds = st, \({D}^{2}s = s(1 + 2{t}^{2})\), and in general D n s = sQ n (t), where Q n (t) is a polynomial of degree n in t. The Secant numbers are given by S k = Q 2k (0). Let \(Q_{n}(t) =\sum _{k\geq 0}q_{n,k}{t}^{k}\). From

$$\displaystyle{D(s{t}^{k}) = s{t}^{k+1} + ks{t}^{k-1}(1 + {t}^{2})}$$

we obtain the three-term recurrence

$$\displaystyle{ q_{n+1,k} = kq_{n,k-1} + (k + 1)q_{n,k+1}\;\;\mathrm{for}\;\;1 \leq k \leq n. }$$
(8.25)

By avoiding the computation of terms q n, k that are known to be zero (n + k odd), and ordering the computation in a manner analogous to that used for Algorithm TangentNumbers, we obtain Algorithm SecantNumbers (see Fig. 8.4), which computes the Secant numbers in place using non-negative integer arithmetic.

Fig. 8.4
figure 4

Algorithm SecantNumbers

8.6.3 Comparison with Atkinson’s Algorithm

Atkinson [2] gave an elegant algorithm for computing both the Tangent numbers \(T_{1},T_{2},\ldots,T_{n}\) and the Secant numbers \(S_{0},S_{1},\ldots,S_{n}\) using a “Pascal’s triangle” style of algorithm that only involves additions of non-negative integers. Since a triangle with 2n + 1 rows in involved, Atkinson’s algorithm requires 2n 2 + O(n) integer additions. This can be compared with \({n}^{2}/2 + O(n)\) additions and n 2 + O(n) multiplications (by small integers) for our Algorithm TangentNumbers, and similarly for Algorithm SecantNumbers.

Thus, we might expect Atkinson’s algorithm to be slower than Algorithm TangentNumbers. Computational experiments confirm this. With n = 1, 000, Algorithm TangentNumbers programmed in Magma takes 1. 50 s on a 2.26 GHz Intel Core 2 Duo, algorithm SecantNumbers also takes 1. 50 s, and Atkinson’s algorithm takes 4. 51 s. Thus, even if both Tangent and Secant numbers are required, Atkinson’s algorithm is slightly slower. It also requires about twice as much memory.