Problem 36 : Double-base palindromes

Problem Statement

The decimal number, 585 = 10010010012(binary), is palindromic in both bases.

Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.

(Please note that the palindromic number, in either base, may not include leading zeros.)

Solution

def decimalToBinary(n):  
    return bin(n).replace("0b", "")  

def palindrome(x):
    if x==x[::-1]:
        return True
sum1=0  
for i in range(1,10**6):
    if palindrome(str(i)):
        if palindrome(str(decimalToBinary(i))):
            sum1=sum1+i
            
print(sum1)

Output

872187