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
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 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()