Largest Palindrome Product

Author

Angel Alcala Ruiz

Published

November 20, 2023

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.

Solution

Let’s solve this problem using Julia

Code
function IsPalindrome(n)

#' @@name IsPalindrome
#' 
#' @@description 
#' 
#' This functtion checks whether a positve integer n is a palindromic number
#'
#' @@arg n: A positive integer n 
#'
#' @@return true or false  
#'
#' @@examples
#' 
#' n = 1009001
#' IsPalindrome(n)

    function array_to_int(A)
    # This function converts an array to an integer
        n = 0
        l = length(A)
        for i = 1:l
            n += A[i]*10^(l - i)
        end
        return n
    end

    function int_to_array(n)
    # This function converts an integer to an array
        vec = []
        while true
            digit = n%10
            vec = push!(vec,digit)
            n = trunc(Int, floor(n/10))
            if n < 1
                break
            end
        end
        vec = reverse(vec)
        return vec
    end

    # n gets updated so we have to fix m for a condition
    m = n
    bool = false

    vec = int_to_array(n)
    vec = reverse(vec)
    new_n = array_to_int(vec)

    if m == new_n
        bool = true
    end

    return bool
end
IsPalindrome (generic function with 1 method)
Code
function LargestPalindrome(digits)

#' @@name LargestPalindrome
#'
#' @@description
#'
#' This function finds the largest palindrome product of two numbers of either 2, 3, or 4 digits
#'
#' @@arg digits: The number digits of the two numbers being multiplied for the palindrome product
#'
#' @@return The largest palindrome product
#'
#' @@examples
#'
#' digits = 2
#' LargestPalindrome(digits) 

    if digits == 2
        limit = 99
    elseif digits == 3
        limit = 999
    elseif digits == 4
        limit = 9999
    else
        println("Invalid number of digits") 
    end

    largest_palin = 0
    product1 = 0
    product2 = 0

    for i = 1:limit
        for j = 1:limit
            product = i*j
            if IsPalindrome(product)
                if product > largest_palin
                    largest_palin = product
                    product1 = i
                    product2 = j
                end
            end
        end
    end

    return largest_palin, product1, product2
end
LargestPalindrome (generic function with 1 method)

Let’s now test our code with the product of two 2-digit numbers

Code
LargestPalindrome(2)
(9009, 91, 99)

Therefore we can see that the largest palindromic number from the product of two 2-digit numbers is \(9009 = 91 \times 99\).

Now let’s find the largest palindromic number that’s formed by the product of two 3-digit numbers

Code
LargestPalindrome(3)
(906609, 913, 993)

Therefore the largest palindromic number from the product of two 3-digit numbers is \(906609 = 913 \times 993\)

Now let’s just find the largest palindromic number from the product of two 4-digit numbers

Code
LargestPalindrome(4)
(99000099, 9901, 9999)

Therefore the largest palindromic number from the product of two 4-digit numbers is \(99000099 = 9901 \times 9999\).