"""Failure mode: correct arithmetic, wrong indexing. Every value this routine returns is a genuine Bernoulli number. Every one of them is filed under the wrong index: ``bernoulli(0)`` hands back ``B1``, ``bernoulli(4)`` hands back ``B5``. The code is clean, the arithmetic is exact and the values are all real Bernoulli numbers. The shift is there to be read -- ``bernoulli`` returns ``_b(n + 1)`` -- but nothing in the arithmetic looks wrong, so a reviewer skimming for mistakes can pass over it. A comparison against a reference, index by index, does not. Expected checker diagnosis: ``off-by-one``, ``odd-terms``, ``ada-indexing`` and ``one-observation``, with a first divergence at ``n=0``. A shift and Lovelace's labels agree everywhere except ``B0``; here ``B0 = -1/2`` points at the shift. """ from fractions import Fraction from math import comb def _b(n: int) -> Fraction: values = [Fraction(0)] * (n + 1) for m in range(n + 1): values[m] = Fraction(1) for k in range(m): values[m] -= Fraction(comb(m, k) * values[k], m - k + 1) if n == 1: return -values[n] return values[n] def bernoulli(n: int) -> Fraction: """Return B_n -- except the sequence starts one index too late.""" if n < 0: raise ValueError("n must be >= 0") return _b(n + 1)