2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
id: 5900f3701000cf542c50fe83
|
2021-02-06 04:42:36 +00:00
|
|
|
|
title: 'Problem 4: Largest palindrome product'
|
2018-10-10 22:03:03 +00:00
|
|
|
|
challengeType: 5
|
2021-02-06 04:42:36 +00:00
|
|
|
|
forumTopicId: 302065
|
2021-01-13 02:31:00 +00:00
|
|
|
|
dashedName: problem-4-largest-palindrome-product
|
2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --description--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
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.
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --hints--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
`largestPalindromeProduct(2)` should return a number.
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert(typeof largestPalindromeProduct(2) === 'number');
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
`largestPalindromeProduct(2)` should return 9009.
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
|
assert.strictEqual(largestPalindromeProduct(2), 9009);
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
`largestPalindromeProduct(3)` should return 906609.
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
|
assert.strictEqual(largestPalindromeProduct(3), 906609);
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
2020-08-13 15:24:35 +00:00
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
function largestPalindromeProduct(n) {
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
largestPalindromeProduct(3);
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
```js
|
|
|
|
|
const largestPalindromeProduct = (digit)=>{
|
|
|
|
|
let start = 1;
|
|
|
|
|
let end = Number(`1e${digit}`) - 1;
|
|
|
|
|
let palindrome = [];
|
|
|
|
|
for(let i=start;i<=end;i++){
|
|
|
|
|
for(let j=start;j<=end;j++){
|
|
|
|
|
let product = i*j;
|
|
|
|
|
let palindromeRegex = /\b(\d)(\d?)(\d?).?\3\2\1\b/gi;
|
|
|
|
|
palindromeRegex.test(product) && palindrome.push(product);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Math.max(...palindrome);
|
|
|
|
|
}
|
|
|
|
|
```
|