Project Euler Problem 36: DoubleBase Palindromes¶
The source code for this problem can be found here.
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\).
Note
that the palindromic number, in either base, may not include leading zeros.
Solution Discussion¶
This solution simply searches through the integer range and identifies values that are palindromic in bases \(2\) and \(10\). These values are summed to produce the answer.
The one clever component is to only consider odd numbers. Every even integer has \(0\) as its least significant bit which is not allowed for the most significant bit. Therefore no even number is a base \(2\) palindrome.
Solution Implementation¶
from lib.digital import is_palindrome
def solve():
""" Compute the answer to Project Euler's problem #36 """
upper_bound = 1000000
answer = 0
palindromes = filter(lambda n: is_palindrome(n, 10) and is_palindrome(n, 2), range(1, upper_bound, 2))
answer += sum(palindromes)
return answer
expected_answer = 872187

solutions.problem36.
solve
()¶ Compute the answer to Project Euler’s problem #36