-
Notifications
You must be signed in to change notification settings - Fork 0
/
36_double_based_palindromes.py
43 lines (33 loc) · 1.03 KB
/
36_double_based_palindromes.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# https://projecteuler.net/problem=36
# 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.)
import utils
def is_palindrome_in_binary(x):
"""
>>> is_palindrome_in_binary(585)
True
"""
b = "{0:b}".format(x)
# extra work in int conversion but is necessary to use utility function
return utils.is_palindrome(int(b))
def is_palindrome_in_decimal(x):
"""
>>> is_palindrome_in_decimal(585)
True
"""
return utils.is_palindrome(x)
def is_palindrome_in_dec_and_bin(x):
"""
>>> is_palindrome_in_dec_and_bin(585)
True
"""
return is_palindrome_in_decimal(x) and is_palindrome_in_binary(x)
def main():
return sum([i for i in xrange(1, 1000000) if is_palindrome_in_dec_and_bin(i)])
if __name__ == "__main__":
import doctest
doctest.testmod()
print main()