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
functionIsPalindrome(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)functionarray_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)endreturn nendfunctionint_to_array(n)# This function converts an integer to an array vec = []whiletrue digit = n%10 vec =push!(vec,digit) n =trunc(Int, floor(n/10))if n <1breakendend vec =reverse(vec)return vecend# 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 =trueendreturn boolend
IsPalindrome (generic function with 1 method)
Code
functionLargestPalindrome(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 =99elseif digits ==3 limit =999elseif digits ==4 limit =9999elseprintln("Invalid number of digits") end largest_palin =0 product1 =0 product2 =0for i =1:limitfor j =1:limit product = i*jifIsPalindrome(product)if product > largest_palin largest_palin = product product1 = i product2 = jendendendendreturn largest_palin, product1, product2end
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\).