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

In this problem, we need to find the largest palindrome number that can be created by multiplying two n-digit numbers using JavaScript. We'll implement two functions: one to find the largest palindrome product and another to check if a number is palindromic.

What are Palindrome Numbers?

A palindrome number reads the same forwards and backwards. Examples include 44, 121, 202, 404, and 909. When reversed, these numbers remain identical to their original form.

Understanding the Problem

We need to find the largest possible palindrome that results from multiplying two n-digit numbers. For example, with 2-digit numbers (10-99), we want the largest palindromic product from all possible combinations.

Approach

Our strategy involves iterating through all possible combinations of n-digit numbers, starting from the largest. We compute each product and check if it's palindromic, keeping track of the largest palindrome found.

Algorithm Steps

Step 1: Define two functions - largestPalindromeProduct(n) to find the result and isPalindrome(num) to check palindromes.

Step 2: Calculate the maximum n-digit number (e.g., 999 for n=3) and minimum n-digit number (e.g., 100 for n=3).

Step 3: Use nested loops to generate all product combinations, starting from the largest numbers.

Step 4: For each product, check if it's palindromic and larger than the current maximum.

Step 5: Return the largest palindrome found.

Implementation

// 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;
            }
        }
    }

    return largestPalindrome;
}

// Function to check if a 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;
}

// Test with different n-digit numbers
const n2 = 2;
const n3 = 3;

const result2 = largestPalindromeProduct(n2);
const result3 = largestPalindromeProduct(n3);

console.log(`Largest palindrome from two ${n2}-digit numbers: ${result2}`);
console.log(`Largest palindrome from two ${n3}-digit numbers: ${result3}`);

// Show the factors for 2-digit example
console.log("For 2-digit: 99 × 91 =", 99 * 91);
Largest palindrome from two 2-digit numbers: 9009
Largest palindrome from two 3-digit numbers: 906609
For 2-digit: 99 × 91 = 9009

How It Works

The isPalindrome() function converts the number to a string and compares characters from both ends moving inward. The main function systematically checks products starting from the largest possible combinations, ensuring we find the maximum palindrome efficiently.

Optimization Note

Starting the inner loop from i instead of maxNum avoids duplicate calculations (since multiplication is commutative). This reduces the search space significantly.

Time Complexity

The time complexity is O(n²) where n represents the range of n-digit numbers. We use nested loops to generate all possible products, making this a brute-force approach with quadratic complexity.

Conclusion

This solution efficiently finds the largest palindromic product of two n-digit numbers by systematically checking all combinations. The approach uses nested iteration and a helper function to verify palindromes, providing the correct result for any digit length.

Updated on: 2026-03-15T23:19:00+05:30

401 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements