Tim Varley Logo
Tim Varley Principal AI Engineer and Tech Leader
Problem #50 hard

Consecutive Prime Sum

The prime 4141, can be written as the sum of six consecutive primes:

41=2+3+5+7+11+13.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 2121 terms, and is equal to 953953.

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

View on Project Euler

Implementations

cpp
// https://projecteuler.net/problem=50
// 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?
// Answer: 997651
// Authored by: Tim Varley 💘
#include <iostream>
#include <vector>
#include "sieve_eratos.h"
int consecutive_prime_sum() {
const int LIMIT = 1000000;
CSieveOfEratosthenes primes(LIMIT);
std::vector<int> prime_list;
for (int i = 2; i < LIMIT; ++i) {
if (primes.is_prime(i)) prime_list.push_back(i);
}
int max_length = 0;
int result = 0;
for (size_t start = 0; start < prime_list.size(); ++start) {
long long sum = 0;
for (size_t end = start; end < prime_list.size(); ++end) {
sum += prime_list[end];
if (sum >= LIMIT) break;
int length = end - start + 1;
if (length > max_length && primes.is_prime(sum)) {
max_length = length;
result = sum;
}
}
}
return result;
}
#if ! defined UNITTEST_MODE
int main(int argc, char const *argv[]) {
std::cout << consecutive_prime_sum() << std::endl;
}
#endif // UNITTEST_MODE
View on GitHub
O(n) time complexity

Solution Notes

Mathematical Background

The problem asks for the prime below 1,000,000 that can be written as the sum of the most consecutive primes.

Examples:

  • 41 = 2+3+5+7+11+13 (6 primes)
  • 953 = sum of 21 consecutive primes

Algorithm Analysis

Generate all primes below 1,000,000, then for each possible starting index, find the longest consecutive sum that equals a prime.

Use prefix sums for efficient range sum queries.

Performance Analysis

  • Time Complexity: O(n²) in worst case, but optimized with prefix sums
  • Space Complexity: O(n) for prime list and prefix sums
  • Execution Time: Moderate, finds answer quickly
  • Scalability: Quadratic, acceptable for n=10^6

Key Insights

  • The longest sum uses 543 consecutive primes

  • Starts from prime 7, sums to 997651

  • Prefix sums enable O(1) range queries

  • Must check that the sum is prime

Educational Value

This problem teaches:

  • Prime generation and sieves

  • Prefix sum arrays for range queries

  • Optimizing brute force searches

  • Finding maximum length sequences with constraints

Problem #50 is taken from Project Euler and licensed under CC BY-NC-SA 4.0.