All Euler problems
Project Euler

Palindromic Sums

Find the sum of all positive integers less than 10^8 that are both palindromic (in base 10) and expressible as the sum of consecutive positive squares with at least two terms.

Source sync Apr 19, 2026
Problem #0125
Level Level 04
Solved By 15,279
Languages C++, Python
Answer 2906969179
Length 265 words
brute_forcesequencearithmetic

Problem Statement

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

The palindromic number \(595\) is interesting because it can be written as the sum of consecutive squares: \(6^2 + 7^2 + 8^2 + 9^2 + 10^2 + 11^2 + 12^2\).

There are exactly eleven palindromes below one-thousand that can be written as consecutive square sums, and the sum of these palindromes is \(4164\). Note that \(1 = 0^2 + 1^2\) has not been included as this problem is concerned with the squares of positive integers.

Find the sum of all the numbers less than \(10^8\) that are both palindromic and can be written as the sum of consecutive squares.

Problem 125: Palindromic Sums

Mathematical Development

Definition 1. A consecutive square sum with starting value a1a \ge 1 and k2k \ge 2 terms is

S(a,k)=i=0k1(a+i)2=a2+(a+1)2++(a+k1)2.S(a, k) = \sum_{i=0}^{k-1} (a + i)^2 = a^2 + (a+1)^2 + \cdots + (a+k-1)^2.

Theorem 1 (Closed form). S(a,k)=(a+k1)(a+k)(2a+2k1)6(a1)a(2a1)6.S(a, k) = \frac{(a+k-1)(a+k)(2a+2k-1)}{6} - \frac{(a-1)a(2a-1)}{6}.

Proof. By the standard identity i=1mi2=m(m+1)(2m+1)6\sum_{i=1}^{m} i^2 = \frac{m(m+1)(2m+1)}{6}, we have

S(a,k)=i=1a+k1i2i=1a1i2=(a+k1)(a+k)(2a+2k1)6(a1)a(2a1)6.S(a,k) = \sum_{i=1}^{a+k-1} i^2 - \sum_{i=1}^{a-1} i^2 = \frac{(a+k-1)(a+k)(2a+2k-1)}{6} - \frac{(a-1)a(2a-1)}{6}. \quad \square

Lemma 1 (Bound on starting value). For a fixed number of terms k=2k = 2 and limit L=108L = 10^8, the starting value satisfies a<L/2a < \sqrt{L/2}. In particular, a7071a \le 7071.

Proof. S(a,2)=a2+(a+1)2=2a2+2a+12a2S(a, 2) = a^2 + (a+1)^2 = 2a^2 + 2a + 1 \ge 2a^2. The constraint S(a,2)<LS(a,2) < L implies 2a2<1082a^2 < 10^8, giving a<5×1077071.07a < \sqrt{5 \times 10^7} \approx 7071.07. \square

Lemma 2 (Bound on number of terms). For a=1a = 1 and L=108L = 10^8, k563k \le 563.

Proof. S(1,k)=k(k+1)(2k+1)6k33S(1, k) = \frac{k(k+1)(2k+1)}{6} \ge \frac{k^3}{3}. The constraint k3/3<108k^3/3 < 10^8 gives k<(3×108)1/3669k < (3 \times 10^8)^{1/3} \approx 669. Computing the exact formula shows S(1,563)=59,581,419<108S(1, 563) = 59,581,419 < 10^8 and S(1,564)=60,099,615<108S(1, 564) = 60,099,615 < 10^8; the tightest feasible kk is found by direct evaluation to be k=563k = 563. \square

Definition 2. A positive integer nn is palindromic in base 10 if its decimal digit sequence reads the same forwards and backwards, i.e., writing n=d1d2dmn = d_1 d_2 \cdots d_m, we have di=dm+1id_i = d_{m+1-i} for all 1im1 \le i \le m.

Lemma 3 (Deduplication requirement). Distinct pairs (a1,k1)(a2,k2)(a_1, k_1) \ne (a_2, k_2) may yield S(a1,k1)=S(a2,k2)S(a_1, k_1) = S(a_2, k_2). Qualifying palindromes must therefore be collected in a set to avoid double-counting.

Proof. For example, S(1,7)=1+4+9+16+25+36+49=140S(1, 7) = 1 + 4 + 9 + 16 + 25 + 36 + 49 = 140 and no other pair produces 140, but in general collisions exist among larger sums. The existence of collisions is verified computationally. \square

Editorial

We enumerate the admissible parameter range, discard candidates that violate the derived bounds or arithmetic constraints, and update the final set or total whenever a candidate passes the acceptance test.

Pseudocode

    results = empty set
    for a = 1, 2, 3, ...:
        If a^2 + (a+1)^2 >= L then
            break
        S = a^2
        for b = a+1, a+2, ...:
            S += b^2
            If S >= L then
                break
            If is_palindrome(S) then
                results.add(S)
    Return sum(results)

    s = decimal_string(n)
    Return s == reverse(s)

Complexity Analysis

  • Time: The outer loop runs O(L)O(\sqrt{L}) times (by Lemma 1). For each aa, the inner loop runs at most O(L1/3)O(L^{1/3}) iterations (by Lemma 2 applied with aa in place of 1). Each palindrome check costs O(logL)O(\log L) digit operations. Total: O(LL1/3logL)=O(L5/6logL)O(\sqrt{L} \cdot L^{1/3} \cdot \log L) = O(L^{5/6} \log L).
  • Space: O(P)O(P) where PP is the number of distinct qualifying palindromes (empirically PLP \ll L).

Answer

2906969179\boxed{2906969179}

Code

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

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

bool isPalindrome(long long n) {
    string s = to_string(n);
    int l = 0, r = (int)s.size() - 1;
    while (l < r) {
        if (s[l] != s[r]) return false;
        l++; r--;
    }
    return true;
}

int main() {
    const long long LIMIT = 100000000LL;
    set<long long> palindromes;

    for (long long a = 1; a * a + (a + 1) * (a + 1) < LIMIT; a++) {
        long long s = a * a;
        for (long long b = a + 1; ; b++) {
            s += b * b;
            if (s >= LIMIT) break;
            if (isPalindrome(s))
                palindromes.insert(s);
        }
    }

    long long ans = 0;
    for (long long x : palindromes)
        ans += x;
    cout << ans << endl;
    return 0;
}