Skip to content

Instantly share code, notes, and snippets.

@chris373
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save chris373/fd492a205b40026aa6a4 to your computer and use it in GitHub Desktop.

Select an option

Save chris373/fd492a205b40026aa6a4 to your computer and use it in GitHub Desktop.
Brute force solution to ProjectEuler.net question 36
'''
Double-base palindromes
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.)
'''
# Written for Pythong 2.7
leftind = 0
rightind = 0
middle = 0
numAsString = ''
def palindrome(string) :
leftind = 0
rightind = len(string)-1
middle = len(string)/2
isPalidrome = True
while leftind <= middle :
if string[leftind] != string[rightind] :
isPalidrome = False
break
leftind += 1
rightind -= 1
return isPalidrome
palindromicNumbers = []
for i in xrange(1, 1000000) :
if palindrome(str(i)) and palindrome(bin(i)[2::]) :
palindromicNumbers.append(i)
total = sum(palindromicNumbers)
print total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment