Project Euler - Problem 4 - Largest Palindrome Product: Coding Interview Prep (freeCodeCamp)

 Problem 4: Largest palindrome product

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 n-digit numbers.


Solution:


function largestPalindromeProduct(n) {
  let maxN = 10**n - 1;
  let minN = 10**(n-1);
  let arr = []

  for(let i=maxN;i>=minN;i--) {
    for(let j=maxN;j>=minN;j--) {
      let prod = i * j;
      isPalin(prod) && arr.push(prod)      
    }
  }

  console.log(Math.max(...arr))

  return Math.max(...arr);
}

function isPalin(n) {
  let num = (n).toString();
  let revNum = num.split("").reverse().join("");

  return num === revNum
}

largestPalindromeProduct(2);


Click here to go to the original link of the question.

Post a Comment

Previous Post Next Post