"""
[ref.] 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.

"""

def ReverseNumber(num):
    if num < 0:
        num = -num
    result = 0
    while num > 0:
        result *= 10
        result += num % 10
        num //= 10
    return result

def IsNumberPalindromic(n):
    return n == ReverseNumber(n)

def FindLargestPalindromicNumber(muldigits):
    minmul = int(10 ** (muldigits - 1))
    maxmul = int(10 ** muldigits - 1)

    mulrange = xrange(minmul, maxmul + 1)

    # since we are looking for the largest palindrome
    # we will start at the top
    rmulrange = reversed(mulrange)

    for mul1 in rmulrange:
        for mul2 in rmulrange:
            product = mul1 * mul2
            if IsNumberPalindromic(product):
                return product
    return -1

digitStart = 1
digitStop  = 6
for digitCount in range(digitStart, digitStop):
    lpn = FindLargestPalindromicNumber(digitCount)
    if lpn == -1:
        ans = "does not exist"
    else:
        ans = "is " + str(lpn)
    print "The largest palindrome made from the product of two %d-digit numbers %s." % (digitCount, ans)

"""
[ref.href] https://projecteuler.net/problem=4

Largest palindrome product.

A palindromic number reads the same both ways. The largest
palindrome made from the product of two 2-digit numbers is
9009 = 91 x 99

Find the largest palindrome made from the product of two
3-digit numbers.
"""

def IsPalindrome(n):
    s = str(n)
    return s == "".join(t for t in reversed(s))

def GetLargestPalindromeProduct(maxdigits):
    a = 1 * 10 ** (maxdigits - 1)
    b = 1 * 10 ** maxdigits - 1
    mx = 0
    for m1 in xrange(a, b + 1):
        for m2 in xrange(a, b + 1):
            product = m1 * m2
            if IsPalindrome(product) and product > mx:
                mx = product
    return mx

maxdigits = 3
maxpali = GetLargestPalindromeProduct(maxdigits)
print "The largest palindrome made from the product of two " +\
      str(maxdigits) + "-digit numbers is " + str(maxpali) + "."

Update problem4.py