24 lines
629 B
Python
24 lines
629 B
Python
|
|
def gen_bit(bit_len):
|
|
for i in range(2 ** ((bit_len + 1) // 2)):
|
|
unit = bin(i)[2:]
|
|
half = '0' * ((bit_len + 1) // 2 - len(unit)) + unit
|
|
if bit_len % 2:
|
|
yield half + half[-2::-1]
|
|
else:
|
|
yield half + half[::-1]
|
|
|
|
def count_palindromic(limit):
|
|
bit_len = 1
|
|
result = []
|
|
while True:
|
|
for s in gen_bit(bit_len):
|
|
num = eval('0b1' + s + '1')
|
|
if num > limit:
|
|
return [1, 3] + result
|
|
if str(num) == str(num)[::-1]:
|
|
result.append(num)
|
|
bit_len += 1
|
|
|
|
print(sum(count_palindromic(1000000)))
|