princeofsudan

ProjectEuler#36 Double-base palindromes

Mar 20th, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.61 KB | None | 0 0
  1. #The decimal number, 585 = 1001001001 (binary), is palindromic in both bases.
  2.  
  3. #Find the sum of all numbers, less than one million, which are palindromic
  4. #in base 10 and base 2.
  5.  
  6. #(Please note that the palindromic number, in either base,
  7. #may not include leading zeros.)
  8.  
  9.  
  10. def decToBin(n):
  11.     if n==0: return ''
  12.     else:
  13.         return decToBin(n/2) + str(n%2)
  14.  
  15. def isPal(s):
  16.     if len(s) <= 1:
  17.         return True
  18.     else:
  19.         return s[0] == s[-1] and isPal(s[1:-1])
  20.  
  21. limit = 1000000
  22. count = 0
  23. for i in range(limit+1):
  24.     if isPal(str(i)) and isPal(decToBin(i)):
  25.         count += i
  26. print count
Advertisement
Add Comment
Please, Sign In to add comment