summaryrefslogtreecommitdiff
path: root/125.py
blob: 35dae6b4d46c7c2d3f390de5a6697c38f9c031b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

limit = 10**8

squares = [ x*x for x in xrange(1, 7200) ]

def is_palindrome(n):
    nstr = str(n)
    return nstr == nstr[::-1]


palindromes = set()

for i in xrange(len(squares)-1):    # iterate over all starting positions
    for j in xrange(2, len(squares)):	# iterate over all possible lengths
	sqrsum = sum(squares[i:i+j])
	if sqrsum >= limit:
	    break
	if is_palindrome(sqrsum):
	    palindromes.add(sqrsum)

print sum(palindromes)