In this blog, I go though how I solved the Double-base palindromes problem from Project Euler.
The problem first describes what is a double-base palindrome — it a number where both its base 10 form and binary (base 2) form are palindromes. It then asks for the sum of all numbers below one million that are double-base palindromes.
In my approach, this problem can be broken down into 2 steps, where the first step produces a list of base10 palindromes, and the second step checks if the binary form of each individual element in the previous list is also a palindrome. The output of the second step would then allow me to meet the final goal.
For the first step, since a palindrome is a number that reads the same backwards as forwards. I can validate a number as a palindrome by inverting the sequence of its string form, as shown using the following code snippet:
The process is repeated on every single number from 1 to 1000000, where all identified palindromes are stored in a list that will be further validated in the second step.
In the second step, I have a variable that keeps track of the total sum. The sum gets updated whenever a binary form of a number from the previous list is found to be palindromic using the same logic. This idea is illustrated in the code snippet below:
In the code above, sum_palindromes is used to keep track of the sum. Within the for loop, each number is first converted to its binary form before getting inversed.
The complete implementation can be found in the notebook below.