Python Implementation of Problem 36

View source code here on GitHub!

Includes

Problem Solution

Project Euler Problem 36

This one ended up being very easy thanks to the datetime library

Problem:

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.

(Please note that the palindromic number, in either base, may not include leading zeros.)

python.src.p0036.bin_repr(x: int) str
python.src.p0036.main() int
 1"""
 2Project Euler Problem 36
 3
 4This one ended up being very easy thanks to the datetime library
 5
 6Problem:
 7
 8The decimal number, 585 = 10010010012 (binary), is palindromic in both bases.
 9
10Find the sum of all numbers, less than one million, which are palindromic in
11base 10 and base 2.
12
13(Please note that the palindromic number, in either base, may not include
14leading zeros.)
15
16"""
17from .lib.utils import is_palindrome
18
19
20def bin_repr(x: int) -> str:
21    return bin(x)[2:]
22
23
24def main() -> int:
25    answer = 0
26    for x in range(1, 1000000):
27        if is_palindrome(x) and is_palindrome(x, rep_func=bin_repr):
28            answer += x
29    return answer

Tags: palindrome, number-base