Problem 145

completed January 22, 2012

Comments

This one is far from being optimal. I really didn’t know what else to do because I’ve tried it like four different ways and it still takes forever for it to run. So, I just walked away from the computer for three hours and found the answer waiting when I got back.

Code

from time import time
start = time(); end = time()
bound = 10**9

def rev(n):
	n = str(n)
	if n[-1] == '0':
		return None
	ans = []
	for i in range(len(n)):
		ans.append(n[-1-i])
	return int(''.join(map(str, ans)))

odds = ['1','3','5','7','9']
def isodd(n):
	global odds
	for d in str(n):
		if d not in odds:
			return False
	return True


count = 0
for n in xrange(bound):
	if n%10**6 == 0: 
		end, start = time() - start, time()	
		print n/10**6, end
	reverse = rev(n)
	if reverse == None:
		continue
	elif isodd(n + reverse) == True:
		count += 1

print count