Find the largest palindrome number made from the product of two n digit numbers in JavaScript


In the given problem statement we have to find the largest palindrome number made from the product of two n digit numbers with the help of Javascript functionalities. So we will create two functions to do this task. First function is to find the largest palindrome number and the second function is to check that the number is palindrome.

What are Palindrome Numbers ?

In the given problem statement there is the usage of the word palindrome !! Let’s first understand the meaning of this word. Palindrome is the term used when we have to define a string or numbers which looks the same after reversing it. For example we have numbers like 44, 121, 202, 404, 909, if we reverse these numbers and after reversing - 44, 121, 202, 404, 909, it looks similar to the input string. So this is known as palindrome.

Understanding the Problem

The problem at hand is to find the largest possible palindrome number with the help of Javascript. And our main task is to find the largest palindrome number which should be the product of the two n-digit numbers.

Logic for the given Problem

To solve this problem we will iterate all the possible combinations of n-digit numbers. So we will start from the largest possible number. Then we will compute the multiplication of each pair and check that the number is palindrome or not. If the number is palindrome then we will compare it with the largest palindrome we have found so far. And we will update its value. So we will use nested loops and one more function to check the palindromes.

Algorithm

Step 1: As we have to find the largest palindrome number which should be the product of two digit numbers. So for doing this task we will define two functions. The first function will find the largest palindrome product and the second function will find whether the number is palindrome or not. The first function will accept an argument n which is the number of digits for the two factors. And the second function will accept an argument of the number for which we have to check that the number is palindrome.

Step 2: The first function will initialize the maximum n-digit number and maxNum and minimum n-digit number as minNum.

Step 3: After defining the variables we will use a loop to traverse through the number from maxNum to minNum and also we will check their products with the help of a nested loop.

Step 4: In this step if we have found the palindrome product and if it is greater than the current largest palindrome then we will update it to the largestPalindrome variable.

Step 5: In the last step of this function we will have the largest palindrome number.

Step 6: Inside the second function, first we will convert the given number into a string and then we will compare the characters at the starting and at the end of the string.

Step 7: If the characters of the string are not matched then we will return the value as false which will indicate that the number is not a palindrome number. And if all the characters are matched then we will return the value as true which indicates that the number is a palindrome number.

Example

//Function to find the largest palindrome product
function largestPalindromeProduct(n) {
   const maxNum = Math.pow(10, n) - 1;
   const minNum = Math.pow(10, n - 1);
   let largestPalindrome = 0;

   for (let i = maxNum; i >= minNum; i--) {
      for (let j = i; j >= minNum; j--) {
         const product = i * j;
         if (isPalindrome(product) && product > largestPalindrome) {
            largestPalindrome = product;
            break;
         }
      }
   }

   return largestPalindrome;
}

//Function to check that the number is palindrome
function isPalindrome(num) {
   const str = num.toString();
   const len = str.length;

   for (let i = 0; i < Math.floor(len / 2); i++) {
      if (str[i] !== str[len - 1 - i]) {
         return false;
      }
   }

   return true;
}

const n = 3;
const n1 = 2;
const largestPalindrome = largestPalindromeProduct(n);
const largestPalindrome1 = largestPalindromeProduct(n1);
console.log(`The largest palindrome made from the product of two ${n}-
digit numbers is: ${largestPalindrome}`);
console.log(`The largest palindrome made from the product of two ${n1}-
digit numbers is: ${largestPalindrome1}`);

Output

The largest palindrome made from the 
product of two 3-digit numbers is: 906609
The largest palindrome made from the product of two 
2-digit numbers is: 9009

Complexity

The time complexity for finding the largest palindrome number which is made up of the product of two n digit numbers is O(n^2), in which n is the number of iterations done in the loop. The reason for the complexity is that we have used nested loops.

Conclusion

In the code we have provided, efficiently solves the given problem of finding the largest palindrome number made from the product of two n-digit numbers. We have iterated all the possible pairs of n-digit numbers and keep track of the largest palindrome, so the code gets the desired result. We have used nested loops and a helper function to check that the number is palindrome or not.

Updated on: 14-Aug-2023

199 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements