-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem36.py
More file actions
29 lines (20 loc) · 915 Bytes
/
Problem36.py
File metadata and controls
29 lines (20 loc) · 915 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
"""
The decimal number, 585 = 1001001001 (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.)
"""
if __name__ == "__main__":
numbers = []
for i in xrange(1,1000000):
from strings.patterns import is_palindrome
# Convert decimal value to string
decimal_string = str(i)
# Check if its a palindrome
if not is_palindrome(decimal_string):
continue
# Convert decimal value to binary string
binary_string = "{0:b}".format(i)
# Check if its a palindrome
if is_palindrome(binary_string):
numbers.append(i)
print('The sum of all palindromic numbers in base 2 and base 10 is %d'%sum(numbers))