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.
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 and terms is
Theorem 1 (Closed form).
Proof. By the standard identity , we have
Lemma 1 (Bound on starting value). For a fixed number of terms and limit , the starting value satisfies . In particular, .
Proof. . The constraint implies , giving .
Lemma 2 (Bound on number of terms). For and , .
Proof. . The constraint gives . Computing the exact formula shows and ; the tightest feasible is found by direct evaluation to be .
Definition 2. A positive integer is palindromic in base 10 if its decimal digit sequence reads the same forwards and backwards, i.e., writing , we have for all .
Lemma 3 (Deduplication requirement). Distinct pairs may yield . Qualifying palindromes must therefore be collected in a set to avoid double-counting.
Proof. For example, and no other pair produces 140, but in general collisions exist among larger sums. The existence of collisions is verified computationally.
Editorial
The direct object to generate is the sum of consecutive squares, not the palindrome. For each starting point , 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 , 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 times (by Lemma 1). For each , the inner loop runs at most iterations (by Lemma 2 applied with in place of 1). Each palindrome check costs digit operations. Total: .
- Space: where is the number of distinct qualifying palindromes (empirically ).
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#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;
}
"""
Problem 125: Palindromic Sums
Find the sum of all numbers < 10^8 that are palindromic and
expressible as a sum of consecutive squares (>= 2 terms).
"""
def is_palindrome(n):
s = str(n)
return s == s[::-1]
def solve():
LIMIT = 10**8
palindromes = set()
a = 1
while a * a + (a + 1) * (a + 1) < LIMIT:
s = a * a
b = a + 1
while True:
s += b * b
if s >= LIMIT:
break
if is_palindrome(s):
palindromes.add(s)
b += 1
a += 1
print(sum(palindromes))
solve()