2018-09-30 23:01:58 +01:00
|
|
|
|
---
|
|
|
|
|
id: 5900f3701000cf542c50fe83
|
|
|
|
|
challengeType: 5
|
|
|
|
|
title: 'Problem 4: Largest palindrome product'
|
2019-08-05 09:17:33 -07:00
|
|
|
|
forumTopicId: 302065
|
2018-09-30 23:01:58 +01:00
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## Description
|
|
|
|
|
<section id='description'>
|
2020-02-28 21:39:47 +09:00
|
|
|
|
|
2018-09-30 23:01:58 +01: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.
|
2020-02-28 21:39:47 +09:00
|
|
|
|
|
|
|
|
|
Find the largest palindrome made from the product of two `n`-digit numbers.
|
|
|
|
|
|
2018-09-30 23:01:58 +01:00
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Instructions
|
|
|
|
|
<section id='instructions'>
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
|
|
```yml
|
2018-10-04 14:37:37 +01:00
|
|
|
|
tests:
|
2020-02-28 21:39:47 +09:00
|
|
|
|
- text: <code>largestPalindromeProduct(2)</code> should return a number.
|
|
|
|
|
testString: assert(typeof largestPalindromeProduct(2) === 'number');
|
2018-10-04 14:37:37 +01:00
|
|
|
|
- text: <code>largestPalindromeProduct(2)</code> should return 9009.
|
2019-07-26 19:41:55 -07:00
|
|
|
|
testString: assert.strictEqual(largestPalindromeProduct(2), 9009);
|
2018-10-04 14:37:37 +01:00
|
|
|
|
- text: <code>largestPalindromeProduct(3)</code> should return 906609.
|
2019-07-26 19:41:55 -07:00
|
|
|
|
testString: assert.strictEqual(largestPalindromeProduct(3), 906609);
|
2018-09-30 23:01:58 +01:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
function largestPalindromeProduct(n) {
|
2020-09-15 09:57:40 -07:00
|
|
|
|
|
2018-09-30 23:01:58 +01:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
largestPalindromeProduct(3);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```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);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</section>
|