top of page

Project Euler #36: Double-Base Palindromes in Python 🔢
Dec 17, 2025
1 min read
0
0
0

Problem: Find the sum of all numbers less than one million which are palindromic in both base 10 and base 2 (binary). Note that palindromic numbers can't include leading zeros in either base.
My approach was straightforward - check every number up to 1,000,000:
python
double_base_palindromes = []
for i in range(0, 1000000):
    y = list(str(i))
    y.reverse()
    x = ''.join(y)
    if int(x) == int(i):
        b = bin(i)
        b = b[2:]
        c = list(b)
        c.reverse()
        d = ''.join(c)
        if int(d) == int(b):
            h = list(d)
            if int(h[0]) == 1:
                print(i)
                double_base_palindromes.append(i)
print(sum(double_base_palindromes))The logic:
Convert each number to a string and reverse it to check if it's a base-10 palindrome
For palindromes, convert to binary using bin()Â and strip the '0b' prefix
Reverse the binary string and compare - if they match, it's a binary palindrome too
Verify the first digit is 1 (ensuring no leading zeros in binary)
Sum all qualifying numbers
Sometimes the brute-force approach is the clearest path to the solution!
Related Posts
Comments
Share Your ThoughtsBe the first to write a comment.
bottom of page





