Let us consider the number 9009. This is a special number in the sense that this is the largest palindrome number that can be formed by multiplying two 2-digit numbers (91 and 99).
We are required to write a JavaScript function that takes a number n (which specifies the number of digits). The function should simply find and return the largest palindrome number that can be formed by the multiplication of two n digit numbers.
Example
Following is the code −
const largestPalindromic = num => {
let i, n, m, d, max, sup, limit, number = 0;
for (i = 1; i < num; i += 1) {
number = 10 * number + 9;
};
max = number;
sup = 10 * number + 9;
const isPalindromic = n => {
let p = 0, q = n, r;
while (n > 0) {
r = n % 10;
p = 10 * p + r;
n = Math.floor(n / 10);
};
return p === q;
};
for (n = sup * sup, m = max * max; n > m; n -= 1) {
if (isPalindromic(n)) {
limit = Math.ceil(Math.sqrt(n));
d = sup;
while (d >= limit) {
if (n % d === 0 && n / d > max) {
return n;
}
d -= 1;
}
}
};
}
console.log(largestPalindromic(3));Output
Following is the output on console −
906609