top of page

Project Euler #36: Double-Base Palindromes in Python 🔢

Dec 17, 2025

1 min read

0

0

0

ree

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:

  1. Convert each number to a string and reverse it to check if it's a base-10 palindrome

  2. For palindromes, convert to binary using bin() and strip the '0b' prefix

  3. Reverse the binary string and compare - if they match, it's a binary palindrome too

  4. Verify the first digit is 1 (ensuring no leading zeros in binary)

  5. Sum all qualifying numbers

Sometimes the brute-force approach is the clearest path to the solution!


#ProjectEuler #Python #ProblemSolving #DataEngineering

Dec 17, 2025

1 min read

0

0

0

Related Posts

Comments

Share Your ThoughtsBe the first to write a comment.
bottom of page