Double-base Palindromes¶
Python's bin function converts an integer to a string representing the number in binary, starting with 0b, so we'll just slice that off before we reverse and check if it's a palindrome.
In [1]:
is_base_2_palindrome = lambda x: bin(x)[2:] == bin(x)[:1:-1]
is_base_10_palindrome = lambda x: str(x) == str(x)[::-1]
double_palindromes = {n for n in range(1, 1000000) if is_base_2_palindrome(n) and is_base_10_palindrome(n)}
double_palindromes
Out[1]:
{1,
3,
5,
7,
9,
33,
99,
313,
585,
717,
7447,
9009,
15351,
32223,
39993,
53235,
53835,
73737,
585585}
In [2]:
sum(double_palindromes)
Out[2]:
872187
Relevant sequences¶
- Numbers that are base-2 and base-10 palindromes: A007632
Copyright (C) 2025 filifa¶
This work is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license and the BSD Zero Clause license.