2018-09-30 22:01:58 +00:00
---
id: 5900f3971000cf542c50feaa
title: 'Problem 43: Sub-string divisibility'
2020-11-27 18:02:05 +00:00
challengeType: 5
2019-08-05 16:17:33 +00:00
forumTopicId: 302100
2021-01-13 02:31:00 +00:00
dashedName: problem-43-sub-string-divisibility
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
The number, 1406357289, is a 0 to 9 pandigital number because it is made up of each of the digits 0 to 9 in some order, but it also has a rather interesting sub-string divisibility property.
2020-02-28 12:39:47 +00:00
2021-06-08 06:13:07 +00:00
Let $d_1$ be the $1^{st}$ digit, $d_2$ be the $2^{nd}$ digit, and so on. In this way, we note the following:
2020-02-28 12:39:47 +00:00
2021-06-08 06:13:07 +00:00
- ${d_2}{d_3}{d_4} = 406$ is divisible by 2
- ${d_3}{d_4}{d_5} = 063$ is divisible by 3
- ${d_4}{d_5}{d_6} = 635$ is divisible by 5
- ${d_5}{d_6}{d_7} = 357$ is divisible by 7
- ${d_6}{d_7}{d_8} = 572$ is divisible by 11
- ${d_7}{d_8}{d_9} = 728$ is divisible by 13
- ${d_8}{d_9}{d_{10}} = 289$ is divisible by 17
2020-02-28 12:39:47 +00:00
2021-06-08 06:13:07 +00:00
Find the sum of all 0 to `n` pandigital numbers with sub-strings fulfilling `n - 2` of these divisibility properties.
**Note:** Pandigital numbers starting with `0` are to be considered in the result.
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-08 06:13:07 +00:00
`substringDivisibility(5)` should return a number.
```js
assert(typeof substringDivisibility(5) === 'number');
```
`substringDivisibility(5)` should return `12444480` .
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
2021-06-08 06:13:07 +00:00
assert.strictEqual(substringDivisibility(5), 12444480)
2020-11-27 18:02:05 +00:00
```
2018-09-30 22:01:58 +00:00
2021-06-08 06:13:07 +00:00
`substringDivisibility(7)` should return `1099210170` .
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
2021-06-08 06:13:07 +00:00
assert.strictEqual(substringDivisibility(7), 1099210170)
2018-09-30 22:01:58 +00:00
```
2021-06-08 06:13:07 +00:00
`substringDivisibility(8)` should return `1113342912` .
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
2021-06-08 06:13:07 +00:00
assert.strictEqual(substringDivisibility(8), 1113342912)
```
`substringDivisibility(9)` should return `16695334890` .
```js
assert.strictEqual(substringDivisibility(9), 16695334890)
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-08 06:13:07 +00:00
function substringDivisibility(n) {
2020-09-15 16:57:40 +00:00
2021-06-08 06:13:07 +00:00
return true;
2018-09-30 22:01:58 +00:00
}
2021-06-08 06:13:07 +00:00
substringDivisibility(5);
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-08 06:13:07 +00:00
function substringDivisibility(n) {
function isSubDivisable(digits) {
const factors = [2, 3, 5, 7, 11, 13, 17];
for (let i = 1; i < digits.length - 2 ; i + + ) {
const subNumber = digits[i] * 100 + digits[i + 1] * 10 + digits[i + 2];
if (subNumber % factors[i - 1] !== 0) {
return false;
}
}
return true;
}
function heapsPermutations(k, digits, conditionCheck, results) {
if (k === 1) {
if (conditionCheck(digits)) {
const number = parseInt(digits.join(''), 10);
results.push(number);
}
return;
}
heapsPermutations(k - 1, digits, conditionCheck, results);
for (let i = 0; i < k - 1 ; i + + ) {
if (k % 2 === 0) {
[digits[i], digits[k - 1]] = [digits[k - 1], digits[i]];
} else {
[digits[0], digits[k - 1]] = [digits[k - 1], digits[0]];
}
heapsPermutations(k - 1, digits, conditionCheck, results);
}
return;
}
const allowedDigits = [...new Array(n + 1).keys()];
const divisablePandigitals = [];
heapsPermutations(
allowedDigits.length,
allowedDigits,
isSubDivisable,
divisablePandigitals
);
let sum = 0;
for (let i = 0; i < divisablePandigitals.length ; i + + ) {
sum += divisablePandigitals[i];
}
return sum;
}
2018-09-30 22:01:58 +00:00
```