palindrome-9

The events have started and one of them is Void fnx, the most anticipated Coding Event of the college. This year, one question has been bugging the coders for hours, Help them solve it. It goes as follows:

The palindromic number 595 can be written as the sum of consecutive squares: from 6 to 12.

Find the sum of all the numbers between a and b (both inclusive) that are both palindromic and can be written as the sum of consecutive squares.

Input: Two numbers a and b.

Output: A single number which is the answer to the question.

Constraints: 1 ≤ a < b ≤ 100000000

SAMPLE INPUT
1 10

SAMPLE OUTPUT
5

Explanation

There is only one such number between 1 and 10 which can be written as the sum of consecutive squares i.e. 11 + 22 = 5.

Source: hackerearth | Difficulty: UNKNOWN_DIFFICULTY


Solutions:
from math import *

def palindrome( s ):
	return s[ ::-1 ] == s

def f(N):
	seen = {}
	sqrtN = int( sqrt( N ) )
	for i in range( 1, sqrtN ):
	    s = i * i
	    for j in range( i + 1, sqrtN ):
	        s += j * j
	        if s > N:
	            break
	        if palindrome( str( s ) ):
	            seen[ s ] = True
	return sum( iter( seen ) )
a,b=list(map(int,input().split()))
print(f(b)-f(a-1))


import math

def ispalin(x):
	o=[]
	while x:
		o.append(x%10)
		x/=10
	r=reversed(o)
	for i,j in zip(o,r):
		if i!=j:
			return False
	return True


s,b=input().split()
s,b=(int)(s),(int)(b)

sq=(int)(math.sqrt(b))
a=[]

for i in range(1,10001):
	num=i*i
	for j in range(i+1,sq+1):
		num+=j*j
		if num>b:
			break
		if(num<s):
			continue
		if (ispalin(num) and num not in a):
			a.append(num)

print(sum(a))