## Project Euler 125: Finding square sums that are palindromic

#### Problem Description

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.

#### Analysis

We add sequences of consecutive squares and record those that are uniquely palindromic. Once the sum has exceeded our limit, L, we can break from the current sequence’s starting number and start with the next. We use a set to eliminate duplicate sum-of-squares.

One may believe that utilizing the formula from Problem 6 (See also, Project Euler 6 Solution: ) to calculate the sum of squares from 1 to *n* may yield a faster solution. But simply calculating (and recalculating) sections of the series is much faster and more obvious.

It is unusual for a brute force solution, such as this one, to perform so well when compared to other more efficient, but often less intuitive, methods which typically include arcane calculations or clever array calisthenics. I have yet to see a Python solution that is faster and more concise than the one presented here.

When code is written with a focused intention and a concise pragma, the result is always clear and understandable code.

I seldom find clever ways to find patterns of this nature, which could be completely random, without first looking at the big picture. Computers are good at doing this on a very large scale.

As an analogy, the picture at the right could possibly have a pattern or some logical scenario in context. But looking at the picture on a too small scale may obscure those observations from view. It’s only when we expand our visual perspective that a pattern may reveal itself.

Of course, if it doesn’t reveal itself we may always be tempted to discount a random pattern and keep expanding and searching until a pattern is perceived. This may imprison our thinking to a lifelong quest for order out of chaos where none exists, or exists merely by coincidence.

In summary, stick with brute force for these kind of discrete problems because that’s how the universe would handle it. Well, it actually keeps replacing one brute force for another until it evolves into an optimum solution. But that’s a story for another time.

#### Project Euler 125 Solution

Runs < 0.200 seconds in Python 2.7.Use this link to get the Project Euler 125 Solution Python 2.7 source.

#### Afterthoughts

- Reference: The On-Line Encyclopedia of Integer Sequences (OEIS) A180436: Palindromic numbers which are sum of consecutive squares.
- Function
`is_palindromic`

is listed in Common Functions and Routines for Project Euler - List of palindromic numbers less than one trillion including the starting and ending number in the consecutive sequence. Rare, duplicate palindromes are listed to the side of their companions.
- Takes 381,232 iterations.
- For L = 10
^{6}: 14893023 - For L = 10
^{10}: 593968508531 - For L = 10
^{12}: 153349168442165

*Project Euler 125 Solution last updated*

## Discussion

## No comments yet.