"""euler4.py https://projecteuler.net/problem=4 A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two 3-digit numbers. in python class | Oct 29 2019 """ def is_palindrome(number): """ return True if number is a palindrome base 10 >>> is_palindrome(123) False >>> is_palindrome(121) True """ n_str = str(number) n_reverse = n_str[::-1] return n_str == n_reverse def search(low, high): """ search for a,b between low an high to find palidrome with largest a*b >>> search(10, 99) (91, 99) """ best_product = 1 best_a = 0 best_b = 0 for a in range(low, high+1): for b in range(a, high+1): product = a*b #print("a={}, b={}, product={}".format(a,b,product)) if is_palindrome(product): #print(" palindrome! ") if product > best_product: #print(" NEW BEST ") best_product = product best_a = a best_b = b return (best_a, best_b) def main(): print(" -- project euler 4 --") (a, b) = search(100, 999) print("a, b = ", a, b) if __name__ == '__main__': import doctest doctest.testmod() main()