All Euler problems
Project Euler

Sums of Power Sums

Define the power sum f_k(n) = sum_(i=1)^n i^k. Let S_k(p) = sum_(i=1)^(p-1) f_k(i) mod p where p is prime. Compute sums of S_k(p) for specified values of k and ranges of primes p.

Source sync Apr 19, 2026
Problem #0487
Level Level 18
Solved By 785
Languages C++, Python
Answer 106650212746
Length 326 words
modular_arithmeticnumber_theorysequence

Problem Statement

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

Let \(f_k(n)\) be the sum of the \(k\)<sup>th</sup> powers of the first \(n\) positive integers.

For example, \(f_2(10) = 1^2 + 2^2 + 3^2 + 4^2 + 5^2 + 6^2 + 7^2 + 8^2 + 9^2 + 10^2 = 385\).

Let \(S_k(n)\) be the sum of \(f_k(i)\) for \(1 \le i \le n\). For example, \(S_4(100) = 35375333830\).

What is \(\displaystyle {\sum } (S_{10000}(10^{12}) \bmod p)\) over all primes \(p\) between \(2 \cdot 10^9\) and \(2 \cdot 10^9 + 2000\)?

Problem 487: Sums of Power Sums

Mathematical Foundation

Theorem 1 (Faulhaber’s formula). For any non-negative integer kk and positive integer nn,

fk(n)=1k+1j=0k(k+1j)Bjnk+1jf_k(n) = \frac{1}{k+1} \sum_{j=0}^{k} \binom{k+1}{j} B_j \, n^{k+1-j}

where BjB_j are the Bernoulli numbers defined by B0=1B_0 = 1 and the recurrence

j=0m(m+1j)Bj=0for all m1.\sum_{j=0}^{m} \binom{m+1}{j} B_j = 0 \quad \text{for all } m \ge 1.

Proof. Consider the exponential generating function tet1=n=0Bntnn!\frac{t}{e^t - 1} = \sum_{n=0}^{\infty} B_n \frac{t^n}{n!}. Multiplying both sides by ent1)/(et1)e^{nt} - 1)/(e^t - 1):

i=0n1eit=ent1et1.\sum_{i=0}^{n-1} e^{it} = \frac{e^{nt} - 1}{e^t - 1}.

Expanding both sides in powers of tt and comparing the coefficient of tk/k!t^k/k! yields

i=0n1ik=1k+1j=0k(k+1j)Bjnk+1j.\sum_{i=0}^{n-1} i^k = \frac{1}{k+1}\sum_{j=0}^k \binom{k+1}{j} B_j n^{k+1-j}.

Since fk(n)=i=1nik=i=0nikf_k(n) = \sum_{i=1}^n i^k = \sum_{i=0}^n i^k, and the i=0i=0 term vanishes for k1k \ge 1, the formula holds (with a minor adjustment for k=0k=0). The Bernoulli recurrence follows from setting n=1n=1 in the identity j=0k(k+1j)Bj=(k+1)0k\sum_{j=0}^k \binom{k+1}{j}B_j = (k+1) \cdot 0^k for k1k \ge 1 (since fk(0)=0f_k(0) = 0), giving j=0k(k+1j)Bj=0\sum_{j=0}^k \binom{k+1}{j}B_j = 0. \square

Theorem 2 (Power sums modulo a prime). For a prime pp and integer m>0m > 0,

i=1p1im{1(modp)if (p1)m,0(modp)otherwise.\sum_{i=1}^{p-1} i^m \equiv \begin{cases} -1 \pmod{p} & \text{if } (p-1) \mid m, \\ 0 \pmod{p} & \text{otherwise.} \end{cases}

Proof. The multiplicative group Fp\mathbb{F}_p^* is cyclic of order p1p - 1. Let gg be a generator. If (p1)m(p-1) \mid m, then im=1i^m = 1 for all iFpi \in \mathbb{F}_p^*, so the sum is p11(modp)p - 1 \equiv -1 \pmod{p}.

If (p1)m(p-1) \nmid m, then gm1g^m \ne 1. The sum T=i=1p1im=j=0p2gjmT = \sum_{i=1}^{p-1} i^m = \sum_{j=0}^{p-2} g^{jm} is a geometric series:

T=g(p1)m1gm1=11gm1=0T = \frac{g^{(p-1)m} - 1}{g^m - 1} = \frac{1 - 1}{g^m - 1} = 0

since g(p1)m=(gp1)m=1m=1g^{(p-1)m} = (g^{p-1})^m = 1^m = 1. \square

Theorem 3 (Evaluation of Sk(p)S_k(p)). Substituting Faulhaber’s formula and exchanging sums:

Sk(p)=1k+1j=0k(k+1j)Bji=1p1ik+1j(modp).S_k(p) = \frac{1}{k+1} \sum_{j=0}^{k} \binom{k+1}{j} B_j \sum_{i=1}^{p-1} i^{k+1-j} \pmod{p}.

By Theorem 2, the inner sum i=1p1ik+1j\sum_{i=1}^{p-1} i^{k+1-j} is 1-1 if (p1)(k+1j)(p-1) \mid (k+1-j) and k+1j>0k+1-j > 0, and 00 otherwise. (When j=k+1j = k+1, the exponent is 0, and i=1p11=p11\sum_{i=1}^{p-1} 1 = p - 1 \equiv -1.) Therefore,

Sk(p)1k+10jk+1(p1)(k+1j)(k+1j)Bj(modp).S_k(p) \equiv \frac{-1}{k+1} \sum_{\substack{0 \le j \le k+1 \\ (p-1) \mid (k+1-j)}} \binom{k+1}{j} B_j \pmod{p}.

Proof. Direct substitution and application of Theorem 2. The factor 1/(k+1)1/(k+1) is computed as the modular inverse (k+1)1modp(k+1)^{-1} \bmod p (valid when pk+1p \nmid k+1). \square

Lemma 1 (Bernoulli number recurrence modulo pp). The Bernoulli numbers B0,B1,,BkB_0, B_1, \ldots, B_k can be computed modulo a prime pp using the recurrence

Bm1m+1j=0m1(m+1j)Bj(modp)B_m \equiv -\frac{1}{m+1} \sum_{j=0}^{m-1} \binom{m+1}{j} B_j \pmod{p}

for m1m \ge 1, starting from B0=1B_0 = 1. All arithmetic is in Fp\mathbb{F}_p.

Proof. This is a direct rearrangement of the defining recurrence j=0m(m+1j)Bj=0\sum_{j=0}^m \binom{m+1}{j}B_j = 0, solving for BmB_m. Division by m+1m+1 is valid when pm+1p \nmid m+1; when pm+1p \mid m+1, use the Kummer congruences or handle as a special case. \square

Editorial

f_k(n) = sum_{i=1}^{n} i^k Compute sums of f_k evaluated at various points modulo primes, using Faulhaber’s formulas and Bernoulli numbers. We iterate over each prime p in primes. We then compute Bernoulli numbers B[0..k] mod p. Finally, evaluate S_k(p).

Pseudocode

for each prime p in primes
Compute Bernoulli numbers B[0..k] mod p
Evaluate S_k(p)

Complexity Analysis

  • Time: O(k2)O(k^2) per prime for computing Bernoulli numbers, plus O(k)O(k) per prime for evaluating Sk(p)S_k(p). For PP primes total: O(Pk2)O(P \cdot k^2). If kk is fixed and Bernoulli numbers are precomputed once (when working over Q\mathbb{Q} and then reducing mod pp), the per-prime cost drops to O(k)O(k), giving O(k2+Pk)O(k^2 + P \cdot k) total.
  • Space: O(k)O(k) for the Bernoulli number array.

Answer

106650212746\boxed{106650212746}

Code

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

C++ project_euler/problem_487/solution.cpp
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

ll mod_pow(ll base, ll exp, ll mod) {
    ll result = 1;
    base %= mod;
    while (exp > 0) {
        if (exp & 1) result = result * base % mod;
        base = base * base % mod;
        exp >>= 1;
    }
    return result;
}

ll mod_inv(ll a, ll p) {
    return mod_pow(a, p - 2, p);
}

// Compute Bernoulli numbers B[0..k] mod p
vector<ll> bernoulli_mod(int k, ll p) {
    vector<ll> B(k + 1, 0);
    B[0] = 1;
    for (int m = 1; m <= k; m++) {
        ll s = 0;
        ll c = 1; // C(m+1, j)
        for (int j = 0; j < m; j++) {
            s = (s + c % p * B[j]) % p;
            c = c % p * ((m + 1 - j) % p) % p * mod_inv(j + 1, p) % p;
        }
        B[m] = (p - s % p) % p * mod_inv(m + 1, p) % p;
    }
    return B;
}

// Compute S_k(p) = sum_{i=1}^{p-1} f_k(i) mod p
// Using Faulhaber + Fermat's Little Theorem
ll sum_power_sums(int k, ll p) {
    auto B = bernoulli_mod(k, p);
    ll inv_k1 = mod_inv(k + 1, p);
    ll result = 0;
    ll c = 1; // C(k+1, j)

    for (int j = 0; j <= k; j++) {
        int m = k + 1 - j;
        ll inner;
        if (m == 0) {
            inner = (p - 1) % p;
        } else if (m % (p - 1) == 0) {
            inner = p - 1; // = -1 mod p
        } else {
            inner = 0;
        }
        result = (result + c % p * (B[j] % p) % p * inner) % p;
        c = c % p * ((k + 1 - j) % p) % p * mod_inv(j + 1, p) % p;
    }

    return result % p * inv_k1 % p;
}

int main() {
    // Demonstration: compute S_k(p) for small primes and k values
    vector<ll> primes = {5, 7, 11, 13, 17, 19, 23};
    for (ll p : primes) {
        for (int k = 1; k <= 4; k++) {
            ll val = sum_power_sums(k, p);
            cout << "S_" << k << "(" << p << ") = " << val << endl;
        }
    }
    return 0;
}