def lychrel(number): count = 0 a = reverse_and_add(number) if is_palindrome(a): return False while count < 50 and not is_palindrome(a): a = reverse_and_add(a) if is_palindrome(a): return False count += 1 return True
def compute(limit): """Finds the largest palindrome made from multiplying 2 numbers whose max sizes are limit.""" for i in range(100, limit): for j in range(100, limit): # Cache product result = i * j if is_palindrome(result): yield result
def is_lychrel(n): for i in range(1, 50): #if n % 10 == 0: # return False n += reverse_number(n) if is_palindrome(n): return False return True
def is_lychrel_number(num, iterations=0): if num in lychrel_numbers: return lychrel_numbers[num] new_num = num + reverse(num) if is_palindrome(new_num): is_lychrel = False elif iterations >= 50: is_lychrel = True else: is_lychrel = is_lychrel_number(new_num, iterations + 1) if num < 10000: lychrel_numbers[num] = is_lychrel return is_lychrel
def problem04(): """ Q: Find the largest palindrome made from the product of two 3-digit numbers. A: 906609 """ largest = 0 for num in range(900, 999): for n in range(900, 999): product = num*n if product < largest: continue if helpers.is_palindrome(product): largest = product return largest
def get_palindromic_digits(number_of_digits): """Get all palindromes from the product of two n digit numbers """ hundreds = int('1' + number_of_digits * '0') all_digits = [x for x in range(hundreds)] number_list = list( filter(lambda x: len(str(x)) == number_of_digits, all_digits)) palindromes = {} for each in number_list: remove_each = set(number_list) - set([each]) for every in remove_each: number = each * every if is_palindrome(str(number)) == True: palindromes[number] = [each, every] return palindromes
#!env/bin/python import helpers as h res = 0 for x in range(1,1000000,2): if h.is_palindrome(x): if h.is_palindrome(h.str_base(x,2)): print "B10: {}\tB2: {}".format(x,h.str_base(x,2)) res += x print res
def compute(limit): for i in range(limit): if is_palindrome(i) and is_palindrome(bin(i)[2:]): yield i
def is_dual_palindrome(n): return is_palindrome(n) and is_palindrome(bin(n).lstrip('0b'))
from helpers import get_binary_value, is_palindrome total = 0 for x in range(1, 10**6, 2): if is_palindrome(x) and is_palindrome(get_binary_value(x)): total += x print(total)
# highest_result = 0 for x in range(100, 1000): for y in range(100, 1000): product = x * y if str(product) == str(product)[::-1] and product > highest_result: highest_result = product print(x, y, highest_result) print(highest_result) # # # highest_result = 0 # for x in range(100, 1000): # for y in range(x, 1000): # highest_result = max(highest_result, is_palindrome(x * y)) # # print(highest_result) # # # print(sorted([x * y for x in range(100, 1000) for y in range(x, 1000) if str(x*y) == str(x*y)[::-1]])[-1]) for x in range(999, 100, -1): for y in range(x, 1000): if is_palindrome(x * y): print(x, y, x * y) break else: continue break