Euler Problem 36

Back to overview.

The decimal number, 585 = $1001001001_2$ (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.)

This looks like an easy problem to me. Like really easy. Should we try to brute force first?

In [1]:
def is_palindrome_decimal(n):
    return str(n) == str(n)[::-1]

assert(is_palindrome_decimal(232) == True)
assert(is_palindrome_decimal(2) == True)
assert(is_palindrome_decimal(2322) == False)
In [2]:
def is_palindrome_binary(n):
    s = str(bin(n)[2:])
    return s == s[::-1]

assert(is_palindrome_binary(585) == True)
assert(is_palindrome_binary(3) == True)
In [3]:
s = sum([i for i in range(1000000) if is_palindrome_decimal(i) and is_palindrome_binary(i)])
In [4]:
print(s)
872187