79836931

Date: 2025-12-03 13:23:23
Score: 0.5
Natty:
Report link
def problem0036() -> int:
    """
    { statement of problem 36 deleted; see the original post ... }
    Notes:
        Binary palindromes must end in '1', making them odd.
    """
    from itertools import filterfalse
    # based on my timings, filterfalse is marginally faster than filter

    dec_pal: list[int] = \
        [i for i in range(1, 1_000_000) if str(i) == str(i)[::-1]]
    bin_pal: list[int] = \
        [i for i in range(1,1_000_000,2) if bin(i)[2:] == bin(i)[2:][::-1]]
    # bin() is marginally faster than format()

    return sum(list(filterfalse(lambda x: x not in bin_pal, dec_pal)))

This is not an optimal solution (see https://math.berkeley.edu/~elafandi/euler/p36/ for a very nice one), but it's terse and Pythonic. My results on an M3 MacBook Pro:

[Code/euler_probs]$ py3 0036-0040.py 36 5
Euler problem #36: result == 872187, time: 0.1956004 sec.
Euler problem #36: result == 872187, time: 0.1955303 sec.
Euler problem #36: result == 872187, time: 0.1923485 sec.
Euler problem #36: result == 872187, time: 0.1914679 sec.
Euler problem #36: result == 872187, time: 0.1923018 sec.

regards,

richard

--

Reasons:
  • Blacklisted phrase (1): regards
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: BlueCat