Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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.)
- def decToBin(n):
- if n==0: return ''
- else:
- return decToBin(n/2) + str(n%2)
- def isPal(s):
- if len(s) <= 1:
- return True
- else:
- return s[0] == s[-1] and isPal(s[1:-1])
- limit = 1000000
- count = 0
- for i in range(limit+1):
- if isPal(str(i)) and isPal(decToBin(i)):
- count += i
- print count
Advertisement
Add Comment
Please, Sign In to add comment