arr = []
a, b = 1234, 4321
# for x in range(1, a + 1):
#     for y in range(1, b + 1):
#         arr.append((x * y, x, y))
# arr.sort(key=lambda x: -x[0])

def makePalindrome(number):
    num_str = str(number)
    digits = len(num_str)
    left_idx, right_idx = (digits - 1) // 2, digits // 2
    num_list = list(num_str)
    while left_idx >= 0:
        num_list[right_idx] = num_list[left_idx]
        right_idx += 1
        left_idx -= 1
    return int("".join(num_list))

def findPreviousPalindrome(number):
    start, end = 0, number
    best = float("-inf")
    while start <= end:
        middle = (end - start) // 2 + start
        palindrome_num = makePalindrome(middle)
        if palindrome_num < number:
            best = palindrome_num
            start = middle + 1
        else:
            end = middle - 1
    return best

ok = lambda n: str(n) == str(n)[::-1]

def solve(a, b):
    n = findPreviousPalindrome(b * b)
    while 1:
        print("n :", n)
        for d in range(1, int(n**0.5) + 1):
            if n % d == 0 and d >= a:
                e = n // d
                if e <= b:
                    return n
        n = findPreviousPalindrome(n - 1)
    return  -1
print(solve(a, b))
                    
    

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: