All Euler problems
Project Euler

Consecutive Prime Sum

Which prime, below one million, can be written as the sum of the most consecutive primes?

Source sync Apr 19, 2026
Problem #0050
Level Level 01
Solved By 69,644
Languages C++, Python
Answer 997651
Length 567 words
number_theorysearchlinear_algebra

Problem Statement

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

>The prime \(41\), can be written as the sum of six consecutive primes: \[41 = 2 + 3 + 5 + 7 + 11 + 13.\] This is the longest sum of consecutive primes that adds to a prime below one-hundred.

The longest sum of consecutive primes below one-thousand that adds to a prime, contains \(21\) terms, and is equal to \(953\).

Which prime, below one-million, can be written as the sum of the most consecutive primes?

Problem 50: Consecutive Prime Sum

Mathematical Development

Formal Development

Definition 1 (Consecutive prime sum). Let p1=2,p2=3,p3=5,p_1 = 2, p_2 = 3, p_3 = 5, \ldots denote the primes in ascending order. A consecutive prime sum of length LL starting at index ii is:

S(i,L)=k=ii+L1pk.S(i, L) = \sum_{k=i}^{i+L-1} p_k.

We seek the prime q<106q < 10^6 such that q=S(i,L)q = S(i, L) for some ii and LL, with LL maximized.

Theorem 1 (Prefix sum formulation). Define P0=0P_0 = 0 and Pn=k=1npkP_n = \sum_{k=1}^{n} p_k for n1n \geq 1. Then S(i,L)=Pi+L1Pi1S(i, L) = P_{i+L-1} - P_{i-1}, and each evaluation is O(1)O(1) after O(π(N))O(\pi(N)) preprocessing.

Proof. By telescoping:

S(i,L)=k=ii+L1pk=(k=1i+L1pk)(k=1i1pk)=Pi+L1Pi1.S(i, L) = \sum_{k=i}^{i+L-1} p_k = \left(\sum_{k=1}^{i+L-1} p_k\right) - \left(\sum_{k=1}^{i-1} p_k\right) = P_{i+L-1} - P_{i-1}.

The prefix sums P0,P1,,Pπ(N)P_0, P_1, \ldots, P_{\pi(N)} are computed in a single pass in O(π(N))O(\pi(N)) time. \square

Lemma 1 (Maximum feasible length). Define Lmax=max{L:PL<N}L_{\max} = \max\{L : P_L < N\}. No consecutive prime sum of length exceeding LmaxL_{\max} can be less than NN.

Proof. The sum S(1,L)=PLS(1, L) = P_L uses the LL smallest primes, so S(1,L)S(i,L)S(1, L) \leq S(i, L) for all i1i \geq 1 (since pjp_j is non-decreasing in jj, replacing pkp_k by pk+(i1)p_{k + (i-1)} for k=1,,Lk = 1, \ldots, L can only increase the sum). Therefore, if PLNP_L \geq N, then S(i,L)PLNS(i, L) \geq P_L \geq N for all ii, so no chain of length LL sums to a prime below NN. \square

Theorem 2 (Optimal search strategy). The following algorithm correctly finds the prime below NN that is the longest consecutive prime sum:

  1. Compute LmaxL_{\max} as in Lemma 1.
  2. For L=Lmax,Lmax1,,1L = L_{\max}, L_{\max} - 1, \ldots, 1: for each starting index i1i \geq 1 with S(i,L)<NS(i, L) < N, test whether S(i,L)S(i, L) is prime.
  3. Return the first prime found.

Proof of correctness. The search proceeds in order of decreasing LL. For a given LL, if any S(i,L)S(i, L) is prime (and <N< N), it is optimal among all chains of that exact length (we only need one). Since we try the largest LL first, the first prime found has maximal length. By Lemma 1, no chain longer than LmaxL_{\max} exists, so the search space is complete.

Proof of termination. For L=1L = 1, every starting prime pi<Np_i < N is itself a valid consecutive prime sum of length 1, so the algorithm always terminates. \square

Theorem 3. The prime below 10610^6 expressible as the longest consecutive prime sum is 997651997651. It equals the sum of 543543 consecutive primes starting from p4=7p_4 = 7:

997651=k=4546pk=7+11+13++3931.997651 = \sum_{k=4}^{546} p_k = 7 + 11 + 13 + \cdots + 3931.

Proof. We apply the Sieve of Eratosthenes to generate all primes below 10610^6 (there are π(106)=78,498\pi(10^6) = 78{,}498 such primes). Prefix sums are computed per Theorem 1. The search of Theorem 2 finds:

  • Lmax=546L_{\max} = 546 (since P546<106P_{546} < 10^6 and P547106P_{547} \geq 10^6).
  • For L=546,545,544L = 546, 545, 544: no starting index yields a prime sum below 10610^6.
  • For L=543L = 543: starting at i=4i = 4 (i.e., p4=7p_4 = 7), S(4,543)=P546P3=997651S(4, 543) = P_{546} - P_3 = 997651.

Primality verification. 997651997651 is confirmed prime by the sieve (alternatively, 997651=998\lfloor\sqrt{997651}\rfloor = 998, and trial division by all primes up to 998998 yields no factor).

Optimality. No chain of length 544,545,544, 545, or 546546 yields a prime below 10610^6, and 543543 is the first length for which a prime is found. \square

Editorial

We sieve all primes below the limit and build prefix sums of the prime sequence, which makes every consecutive prime sum a constant-time difference of two prefixes. Candidate chain lengths are then examined in decreasing order; for each length, the starting index is advanced until the corresponding sum reaches the limit, and primality is checked by table lookup. Because the search proceeds from longer chains to shorter ones, the first prime found is the optimal answer.

Pseudocode

Algorithm: Prime Below the Limit Expressible as the Longest Consecutive Prime Sum
Require: A prime bound L.
Ensure: The prime below L that can be written as the sum of the longest run of consecutive primes.
1: Sieve all primes below L and form the prime sequence p_0, p_1, ... together with prefix sums P_m.
2: For candidate lengths ell, examined from longest to shortest, do:
3:     For each admissible start index s, compute X ← P_(s + ell) - P_s.
4:     If X < L and X is prime, return X.

Complexity Analysis

Proposition (Time complexity). The algorithm runs in O(NloglogN+π(N)2)O(N \log \log N + \pi(N)^2) worst-case time, but with early termination, the practical complexity is much smaller.

Proof. The sieve costs O(NloglogN)O(N \log \log N). Prefix sum computation is O(π(N))O(\pi(N)). The nested search loop iterates over lengths from LmaxL_{\max} downward. For each length LL, the inner loop runs at most π(N)L+1\pi(N) - L + 1 iterations, each costing O(1)O(1) (sieve lookup). In the worst case, this gives L=1Lmax(π(N)L+1)=O(π(N)2)\sum_{L=1}^{L_{\max}} (\pi(N) - L + 1) = O(\pi(N)^2).

However, with early termination (stopping as soon as the first prime is found for any length LL, and breaking when LL drops to best_len), the practical runtime is dominated by the sieve. For N=106N = 10^6, the search terminates after examining only a few lengths near Lmax=546L_{\max} = 546. \square

Proposition (Space complexity). O(N)O(N) for the sieve, plus O(π(N))O(\pi(N)) for the prime list and prefix sums. Total: O(N)O(N).

Answer

997651\boxed{997651}

Code

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

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

int main() {
    const int LIMIT = 1000000;

    vector<bool> is_prime(LIMIT, true);
    is_prime[0] = is_prime[1] = false;
    for (int i = 2; i * i < LIMIT; i++)
        if (is_prime[i])
            for (int j = i * i; j < LIMIT; j += i)
                is_prime[j] = false;

    vector<int> primes;
    for (int i = 2; i < LIMIT; i++)
        if (is_prime[i])
            primes.push_back(i);

    int n = primes.size();
    vector<long long> prefix(n + 1, 0);
    for (int i = 0; i < n; i++)
        prefix[i + 1] = prefix[i] + primes[i];

    int best_len = 0;
    int best_prime = 0;

    for (int len = n; len > best_len; len--) {
        for (int start = 0; start + len <= n; start++) {
            long long s = prefix[start + len] - prefix[start];
            if (s >= LIMIT) break;
            if (is_prime[(int)s]) {
                best_len = len;
                best_prime = (int)s;
                break;
            }
        }
    }

    cout << best_prime << endl;
    return 0;
}