2019-07-16 23:09:53 +00:00
|
|
|
"""
|
2018-10-19 12:48:28 +00:00
|
|
|
Double-base palindromes
|
|
|
|
Problem 36
|
|
|
|
The decimal number, 585 = 10010010012 (binary), is palindromic in both bases.
|
|
|
|
|
2019-07-16 23:09:53 +00:00
|
|
|
Find the sum of all numbers, less than one million, which are palindromic in
|
|
|
|
base 10 and base 2.
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-07-16 23:09:53 +00:00
|
|
|
(Please note that the palindromic number, in either base, may not include
|
|
|
|
leading zeros.)
|
|
|
|
"""
|
2018-10-19 12:48:28 +00:00
|
|
|
def is_palindrome(n):
|
2019-07-16 23:09:53 +00:00
|
|
|
n = str(n)
|
|
|
|
|
|
|
|
if n == n[::-1]:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def solution(n):
|
|
|
|
"""Return the sum of all numbers, less than n , which are palindromic in
|
|
|
|
base 10 and base 2.
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-07-16 23:09:53 +00:00
|
|
|
>>> solution(1000000)
|
|
|
|
872187
|
|
|
|
>>> solution(500000)
|
|
|
|
286602
|
|
|
|
>>> solution(100000)
|
|
|
|
286602
|
|
|
|
>>> solution(1000)
|
|
|
|
1772
|
|
|
|
>>> solution(100)
|
|
|
|
157
|
|
|
|
>>> solution(10)
|
|
|
|
25
|
|
|
|
>>> solution(2)
|
|
|
|
1
|
|
|
|
>>> solution(1)
|
|
|
|
0
|
|
|
|
"""
|
|
|
|
total = 0
|
2018-10-19 12:48:28 +00:00
|
|
|
|
2019-08-19 13:37:49 +00:00
|
|
|
for i in range(1, n):
|
2019-07-16 23:09:53 +00:00
|
|
|
if is_palindrome(i) and is_palindrome(bin(i).split("b")[1]):
|
|
|
|
total += i
|
|
|
|
return total
|
2018-10-19 12:48:28 +00:00
|
|
|
|
|
|
|
|
2019-07-16 23:09:53 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
print(solution(int(str(input().strip()))))
|