Verifying an Efficient Algorithm for Computing Bernoulli Numbers
Manuel Eberl, Peter LammichInternational Conference on Interactive Theorem Proving (ITP), 2025.
Abstract
The Bernoulli numbers B_k are a sequence of rational numbers that is ubiquitous in mathematics, but difficult to compute efficiently (compared to e.g. approximating pi).
In 2008, Harvey gave the currently fastest known practical way for computing them: his algorithm computes B_k mod p in time O(p log^(1 + o(1)) p). By doing this for O(k) many small primes p in parallel and then combining the results with the Chinese Remainder Theorem, one recovers the value of B_k as a rational number in O(k² log^(2 + o(1)) k) time. One advantage of this approach is that the expensive part of the algorithm is highly parallelisable and has very low memory requirements. This algorithm still holds the world record with its computation of B_(10^8).
We give a verified efficient LLVM implementation of this algorithm. This was achieved by formalising the necessary mathematical background theory in Isabelle/HOL, proving an abstract version of the algorithm correct, and refining this abstract version down to LLVM using Lammich’s Isabelle-LLVM framework, including many low-level optimisations. The performance of the resulting LLVM code is comparable with Harvey’s original unverified and hand-optimised C++ implementation.
BibTeX
@inproceedings{el25, author="Eberl, Manuel and Lammich, Peter", title="Verifying an Efficient Algorithm for Computing {B}ernoulli Numbers", year="2025", publisher="Leibniz International Proceedings in Informatics", booktitle="16th International Conference on Interactive Theorem Proving (ITP 2025)", editor="Forster, Yannick and Keller, Chantal", address="Dagstuhl, Germany", doi="10.4230/LIPIcs.ITP.2025.35" }