2018-09-30 22:01:58 +00:00
---
id: 5900f3931000cf542c50fea5
title: 'Problem 38: Pandigital multiples'
2020-11-27 18:02:05 +00:00
challengeType: 5
2019-08-05 16:17:33 +00:00
forumTopicId: 302042
2021-01-13 02:31:00 +00:00
dashedName: problem-38-pandigital-multiples
2018-09-30 22:01:58 +00:00
---
2020-11-27 18:02:05 +00:00
# --description--
2020-02-28 12:39:47 +00:00
2018-09-30 22:01:58 +00:00
Take the number 192 and multiply it by each of 1, 2, and 3:
2020-02-28 12:39:47 +00:00
2021-06-05 08:16:40 +00:00
$$\begin{align}
192 × 1 = 192\\\\
192 × 2 = 384\\\\
192 × 3 = 576\\\\
\end{align}$$
2020-02-28 12:39:47 +00:00
2018-09-30 22:01:58 +00:00
By concatenating each product we get the 1 to 9 pandigital, 192384576. We will call 192384576 the concatenated product of 192 and (1, 2, 3).
2020-02-28 12:39:47 +00:00
2018-09-30 22:01:58 +00:00
The same can be achieved by starting with 9 and multiplying by 1, 2, 3, 4, and 5, giving the pandigital, 918273645, which is the concatenated product of 9 and (1, 2, 3, 4, 5).
2020-02-28 12:39:47 +00:00
2021-06-05 08:16:40 +00:00
What is the largest 1 to `k` pandigital `k` -digit number that can be formed as the concatenated product of an integer with (1, 2, ..., `n` ) where `n` > 1?
2020-02-28 12:39:47 +00:00
2020-11-27 18:02:05 +00:00
# --hints--
2018-09-30 22:01:58 +00:00
2021-06-05 08:16:40 +00:00
`pandigitalMultiples(8)` should return a number.
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
2021-06-05 08:16:40 +00:00
assert(typeof pandigitalMultiples(8) === 'number');
2020-11-27 18:02:05 +00:00
```
2018-09-30 22:01:58 +00:00
2021-06-05 08:16:40 +00:00
`pandigitalMultiples(8)` should return `78156234` .
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
2021-06-05 08:16:40 +00:00
assert.strictEqual(pandigitalMultiples(8), 78156234);
```
`pandigitalMultiples(9)` should return `932718654` .
```js
assert.strictEqual(pandigitalMultiples(9), 932718654);
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
# --seed--
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
## --seed-contents--
2018-09-30 22:01:58 +00:00
```js
2021-06-05 08:16:40 +00:00
function pandigitalMultiples(k) {
2020-09-15 16:57:40 +00:00
2018-09-30 22:01:58 +00:00
return true;
}
2021-06-05 08:16:40 +00:00
pandigitalMultiples(8);
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
# --solutions--
2018-09-30 22:01:58 +00:00
```js
2021-06-05 08:16:40 +00:00
function pandigitalMultiples(k) {
function getKDigitConcatenatedProduct(num, k) {
// returns false if concatenated product is not k digits
2018-09-30 22:01:58 +00:00
let concatenatedProduct = num.toString();
2021-06-05 08:16:40 +00:00
for (let i = 2; concatenatedProduct.length < k ; i + + ) {
2018-09-30 22:01:58 +00:00
concatenatedProduct += num * i;
}
2021-06-05 08:16:40 +00:00
return concatenatedProduct.length === k ? concatenatedProduct : false;
2018-09-30 22:01:58 +00:00
}
2021-06-05 08:16:40 +00:00
function is1toKPandigital(num, k) {
2018-09-30 22:01:58 +00:00
const numStr = num.toString();
2021-06-05 08:16:40 +00:00
// check if length is not k
if (numStr.length !== k) {
2018-09-30 22:01:58 +00:00
return false;
}
// check if pandigital
2021-06-05 08:16:40 +00:00
for (let i = k; i > 0; i--) {
2018-09-30 22:01:58 +00:00
if (numStr.indexOf(i.toString()) === -1) {
return false;
}
}
return true;
}
let largestNum = 0;
2021-06-05 08:16:40 +00:00
for (let i = 10 ** Math.floor(k / 2) + 1; i >= 1; i--) {
const concatenatedProduct = getKDigitConcatenatedProduct(i, k);
if (is1toKPandigital(concatenatedProduct, k)) {
const number = parseInt(concatenatedProduct, 10);
if (number > largestNum) {
largestNum = number;
}
2018-09-30 22:01:58 +00:00
}
}
return largestNum;
}
```