def rev(x): out = '' for i in xrange(len(x)): out += x[-1 - i] return out def make(x): if x == 1: return [1,2,3,4,5,6,7,8,9] lenn = 10 ** (x / 2) out = [] for i in xrange(lenn / 10, lenn): a = str(i) b = rev(a) if x % 2: for i in xrange(10): out.append(int(a + str(i) + b)) else: out.append(int(a + b)) return out def test(x): bi = [] while x > 0: bi.append(x % 2) x /= 2 bb = bi[:] bb.reverse() if bi == bb: #print bi, return True else: return False total = 0 for i in xrange(1, 7): for j in make(i): if test(j): #print j total += j print total