diff options
Diffstat (limited to '036.py')
| -rw-r--r-- | 036.py | 40 |
1 files changed, 40 insertions, 0 deletions
@@ -0,0 +1,40 @@ + +binary = [ 2**x for x in range(20) ][::-1] + +def palindromic_decimal(n): + if n == int(str(n)[::-1]): + return True + return False + + +def dec2bin(n): + res = "" + for i in binary: + if n - i > 0 or n == i: + res += "1" + n -= i + else: + if len(res) > 0: # do not add leading 0s + res += "0" + return res + +def bin2dec(n): + res = 0 + for i in range(1, len(n)+1): + if n[-i] == "1": + res += binary[-i] + return res + +def palindromic_binary(n): + if n == bin2dec(dec2bin(n)[::-1]): + return True + return False + +sum = 0 + +for n in xrange(1000000): + if palindromic_decimal(n) and palindromic_binary(n): + sum += n + +print sum + |
