Skip to content

Instantly share code, notes, and snippets.

@jrjames83
Created July 16, 2017 23:55
Show Gist options
  • Save jrjames83/d7c132a43d72fe9e66b1ff3d1be70f5b to your computer and use it in GitHub Desktop.
Save jrjames83/d7c132a43d72fe9e66b1ff3d1be70f5b to your computer and use it in GitHub Desktop.

The decimal number, 585 = 1001001001 base2 (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.)

format(585, 'b')
'1001001001'
'civic' == 'civic'[::-1]
True
[1,2,3][::-1]
[3, 2, 1]
def conv_bin(number):
    return format(number, 'b')

def is_pal(string):
    return string == string[::-1]
vals = list(range(1000000))
vals[-1]
999999
pairs = {str(x) : conv_bin(x) for x in vals}
pairs['585']
'1001001001'
good_ones = {}

for k, v in pairs.items():
    if is_pal(k) and is_pal(v):
        good_ones[k] = v
len(pairs.keys())
1000000
len(good_ones.keys())
20
good_ones
{'0': '0',
 '1': '1',
 '15351': '11101111110111',
 '3': '11',
 '313': '100111001',
 '32223': '111110111011111',
 '33': '100001',
 '39993': '1001110000111001',
 '5': '101',
 '53235': '1100111111110011',
 '53835': '1101001001001011',
 '585': '1001001001',
 '585585': '10001110111101110001',
 '7': '111',
 '717': '1011001101',
 '73737': '10010000000001001',
 '7447': '1110100010111',
 '9': '1001',
 '9009': '10001100110001',
 '99': '1100011'}
sum(list(map(int, list(good_ones.keys()))))
872187
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment