def test_lychrel(num): n = 1 candidate = num while n <= 50: candidate += utils.reverse(candidate) if utils.palindrome(candidate): return False n += 1 return True
def main1(): #fp = open('palindromes.txt','w') cont = 0 ns = count(2) n = ns.next() tot = 0 while n <= 1000: if palindrome(n): cont+=1 #fp.write("%d\n" % (n)) if test_sum(n): print ">", n tot += n n = ns.next() print "total", tot
Can probably build up a hash map at each index if that number will eventually turn into a palindromic number """ CACHE = {} lychrel_count = 0 for n in range(1, 10000): if n in CACHE: continue # an array to hold all the tested values for this number # if the number winds up being a lychrel number, all the other numbers # should be cached as "True" to avoid recomputing in a subsequent iteration m = n rev_add_count = 0 # how many times has it been reversed and added to self attempted = [m] while rev_add_count < 50: m += utils.reverse(m) if utils.palindrome(m): # update cache for each to True for a in attempted: CACHE[a] = True break else: # try to reverse again, and add this number to list of those attempted rev_add_count += 1 attempted.append(m) if n not in CACHE: lychrel_count += 1 print lychrel_count
#!/usr/bin/python import utils if __name__ == "__main__": n = range(1,1000000) p1 = filter(lambda x: utils.palindrome(str(x)), n) nb = map(lambda x: bin(x)[2:], n) p2 = filter(utils.palindrome, nb) p2f = map(lambda x: int(x, 2), p2) nums = set(p1).intersection(set(p2f)) print len(nums) print nums print sum(list(nums))