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 May 21, 2026
Problem #0125
Level Level 04
Solved By 15,279
Languages C++, Python
Answer 2906969179
Length 342 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 a≥1a \ge 1 and k≥2k \ge 2 terms is

S(a,k)=∑i=0k−1(a+i)2=a2+(a+1)2+⋯+(a+k−1)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+k−1)(a+k)(2a+2k−1)6−(a−1)a(2a−1)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+k−1i2−∑i=1a−1i2=(a+k−1)(a+k)(2a+2k−1)6−(a−1)a(2a−1)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, a≤7071a \le 7071.

Proof. S(a,2)=a2+(a+1)2=2a2+2a+1≥2a2S(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×107≈7071.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, k≤563k \le 563.

Proof. S(1,k)=k(k+1)(2k+1)6≥k33S(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/3≈669k < (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=d1d2⋯dmn = d_1 d_2 \cdots d_m, we have di=dm+1−id_i = d_{m+1-i} for all 1≤i≤m1 \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

The direct object to generate is the sum of consecutive squares, not the palindrome. For each starting point aa, the running sum grows monotonically as more squares are appended, so once the limit is crossed there is no reason to continue extending that start. This turns the search into a nested sweep over starting and ending points, with very effective early stopping.

Whenever the current sum is still below 10810^8, the only extra test needed is whether its decimal expansion is palindromic. A set is used to store qualifying values because the same number can arise from different consecutive-square representations, and the final answer counts each palindrome only once.

Pseudocode

Create an empty set for qualifying palindromic sums.

For each starting value a whose first two squares still fit below the limit:
    Begin a running sum with a^2.
    Extend the sum by adding consecutive squares (a+1)^2, (a+2)^2, and so on.
    If the running sum reaches the limit, stop extending this starting point.
    If the running sum is palindromic, insert it into the set.

Return the sum of the distinct values stored in the set.

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(log⁡L)O(\log L) digit operations. Total: O(L⋅L1/3⋅log⁡L)=O(L5/6log⁡L)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 P≪LP \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;
}