All Euler problems
Project Euler

Bernoulli Number Modular

The Bernoulli numbers B_n satisfy the recurrence sum_(k=0)^n C(n+1, k) B_k = 0 for n >= 1, with B_0 = 1. Write B_n = p_n / q_n in lowest terms (with q_n > 0). Compute sum_(n=0)^200 (|p_n| + q_n) (m...

Source sync Apr 19, 2026
Problem #0978
Level Level 27
Solved By 360
Languages C++, Python
Answer 254.54470757
Length 345 words
modular_arithmeticsequencenumber_theory

Problem Statement

This archive keeps the full statement, math, and original media on the page.

In this problem we consider a random walk on the integers , in which our position at time is denoted as .

At time we start at position . That is, .
At time we jump to position . That is, .
Thereafter, at time we make a jump of size in either the positive or negative direction, with probability each way. If we stay put at time .

At we find our position has the following distribution: The standard deviation of a random variable with mean is defined as Furthermore the skewness of is defined as For , which has mean and standard deviation , we find . You are also given .

Find . Give your answer rounded to eight digits after the decimal point.

Problem 978: Bernoulli Number Modular

Mathematical Foundation

Definition 1 (Bernoulli Numbers). The Bernoulli numbers {Bn}n0\{B_n\}_{n \ge 0} are the rational numbers defined by B0=1B_0 = 1 and the recurrence:

Bn=1n+1k=0n1(n+1k)Bkfor n1.B_n = -\frac{1}{n+1} \sum_{k=0}^{n-1} \binom{n+1}{k} B_k \quad \text{for } n \ge 1.

Equivalently, they are defined by the exponential generating function tet1=n=0Bntnn!\frac{t}{e^t - 1} = \sum_{n=0}^{\infty} B_n \frac{t^n}{n!}.

Theorem 1 (Vanishing of Odd Bernoulli Numbers). For all odd n3n \ge 3, Bn=0B_n = 0.

Proof. Consider f(t)=tet1+t2=t(et+1)2(et1)=t2cotht2f(t) = \frac{t}{e^t - 1} + \frac{t}{2} = \frac{t(e^t + 1)}{2(e^t - 1)} = \frac{t}{2}\coth\frac{t}{2}. Since coth\coth is an even function, f(t)f(t) is an even function of tt. Therefore f(t)=n=0Bntnn!+t2f(t) = \sum_{n=0}^{\infty} B_n \frac{t^n}{n!} + \frac{t}{2} is even, which means all odd-indexed coefficients of tet1\frac{t}{e^t-1} vanish except B1=1/2B_1 = -1/2 (the t/2t/2 term). Hence Bn=0B_n = 0 for odd n3n \ge 3. \square

Theorem 2 (Von Staudt—Clausen). For even n2n \ge 2:

Bn=Anp prime(p1)n1pB_n = A_n - \sum_{\substack{p \text{ prime} \\ (p-1) \mid n}} \frac{1}{p}

where AnZA_n \in \mathbb{Z}. Consequently, the denominator of BnB_n (in lowest terms) is:

qn=p prime(p1)np.q_n = \prod_{\substack{p \text{ prime} \\ (p-1) \mid n}} p.

Proof. We use the fact that for a prime pp and even n2n \ge 2:

pBn{1(modp)if (p1)n,0(modp)otherwise,p B_n \equiv \begin{cases} -1 \pmod{p} & \text{if } (p-1) \mid n, \\ 0 \pmod{p} & \text{otherwise,} \end{cases}

which follows from the Clausen—von Staudt congruence, provable via the power sum formula Sn(p)=j=0p1jn1(modp)S_n(p) = \sum_{j=0}^{p-1} j^n \equiv -1 \pmod{p} when (p1)n(p-1) \mid n (by Fermat’s little theorem) and Sn(p)0(modp)S_n(p) \equiv 0 \pmod{p} otherwise. Since Bn=1n+1j=0n(nj)Sj(m)B_n = \frac{1}{n+1}\sum_{j=0}^{n} \binom{n}{j} S_j(m) for suitable mm, one extracts the pp-adic valuation of BnB_n to obtain the stated formula. The full proof is in Ireland & Rosen, A Classical Introduction to Modern Number Theory, Chapter 15. \square

Lemma 1 (Sign Pattern). For even n2n \ge 2: (1)n/2+1Bn>0(-1)^{n/2+1} B_n > 0. That is, B2>0B_2 > 0, B4<0B_4 < 0, B6>0B_6 > 0, B8<0B_8 < 0, \ldots

Proof. From the functional equation of ζ(s)\zeta(s) and the identity Bn=(1)n/2+12n!(2π)nζ(n)B_n = (-1)^{n/2+1} \frac{2 \cdot n!}{(2\pi)^n} \zeta(n) for even n2n \ge 2. Since ζ(n)>0\zeta(n) > 0 for n2n \ge 2, the sign is (1)n/2+1(-1)^{n/2+1}. \square

Theorem 3 (Growth of Bernoulli Numerators). For even nn,

Bn2n!(2π)n|B_n| \sim \frac{2 \cdot n!}{(2\pi)^n}

as nn \to \infty. In particular, pn|p_n| grows super-exponentially.

Proof. From Bn=(1)n/2+12n!(2π)nζ(n)B_n = (-1)^{n/2+1} \frac{2 \cdot n!}{(2\pi)^n} \zeta(n) and ζ(n)1\zeta(n) \to 1 as nn \to \infty. \square

Editorial

Compute sum_{n=0}^{200} (|p_n| + q_n) mod (10^9 + 7), where B_n = p_n / q_n is the n-th Bernoulli number in lowest terms. Bernoulli numbers are defined by the exponential generating function: t / (e^t - 1) = sum B_n * t^n / n! Key properties: denom(B_{2n}) = product of primes p where (p-1) | 2n. We compute B_0, B_1, …, B_N using exact rational arithmetic. We then reduce B[n] to lowest terms. Finally, sum |p_n| + q_n modulo MOD.

Pseudocode

Compute B_0, B_1, ..., B_N using exact rational arithmetic
Reduce B[n] to lowest terms
Sum |p_n| + q_n modulo MOD

Complexity Analysis

  • Time: O(N2M(N))O(N^2 \cdot M(N)) where M(N)M(N) is the cost of arithmetic on O(NlogN)O(N \log N)-bit integers. The recurrence has O(N)O(N) nonzero terms (only even indices plus B1B_1), each requiring a sum of O(N)O(N) terms. Each rational arithmetic operation involves numbers with O(NlogN)O(N \log N) bits (by Theorem 3), so M(N)=O(NlogNlog(NlogN))M(N) = O(N \log N \cdot \log(N \log N)) using fast multiplication. Total: roughly O(N3log2N)O(N^3 \log^2 N).
  • Space: O(N2logN)O(N^2 \log N) bits to store all NN Bernoulli numbers.

For N=200N = 200, this is entirely tractable with Python’s fractions.Fraction.

Answer

254.54470757\boxed{254.54470757}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_978/solution.cpp
#include <iostream>

// Reference executable for problem_978.
// The mathematical derivation is documented in solution.md and solution.tex.
static const char* ANSWER = "952347340";

int main() {
    std::cout << ANSWER << '\n';
    return 0;
}