2018-09-30 22:01:58 +00:00
---
id: 5900f3b61000cf542c50fec9
title: 'Problem 74: Digit factorial chains'
2020-11-27 18:02:05 +00:00
challengeType: 5
2019-08-05 16:17:33 +00:00
forumTopicId: 302187
2021-01-13 02:31:00 +00:00
dashedName: problem-74-digit-factorial-chains
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 145 is well known for the property that the sum of the factorial of its digits is equal to 145:
2020-02-28 12:39:47 +00:00
2021-05-14 06:18:22 +00:00
$$1! + 4! + 5! = 1 + 24 + 120 = 145$$
2020-02-28 12:39:47 +00:00
2018-09-30 22:01:58 +00:00
Perhaps less well known is 169, in that it produces the longest chain of numbers that link back to 169; it turns out that there are only three such loops that exist:
2020-02-28 12:39:47 +00:00
2021-05-14 06:18:22 +00:00
$$\begin{align}
& 169 → 363601 → 1454 → 169\\\\
& 871 → 45361 → 871\\\\
& 872 → 45362 → 872\\\\
\end{align}$$
2018-09-30 22:01:58 +00:00
It is not difficult to prove that EVERY starting number will eventually get stuck in a loop. For example,
2020-02-28 12:39:47 +00:00
2021-05-14 06:18:22 +00:00
$$\begin{align}
& 69 → 363600 → 1454 → 169 → 363601\\ (→ 1454)\\\\
& 78 → 45360 → 871 → 45361\\ (→ 871)\\\\
& 540 → 145\\ (→ 145)\\\\
\end{align}$$
2020-02-28 12:39:47 +00:00
2018-09-30 22:01:58 +00:00
Starting with 69 produces a chain of five non-repeating terms, but the longest non-repeating chain with a starting number below one million is sixty terms.
2020-02-28 12:39:47 +00:00
2021-05-14 06:18:22 +00:00
How many chains, with a starting number below `n` , contain exactly sixty non-repeating terms?
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-05-14 06:18:22 +00:00
`digitFactorialChains(2000)` should return a number.
```js
assert(typeof digitFactorialChains(2000) === 'number');
```
`digitFactorialChains(2000)` should return `6` .
```js
assert.strictEqual(digitFactorialChains(2000), 6);
```
`digitFactorialChains(100000)` should return `42` .
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
2021-05-14 06:18:22 +00:00
assert.strictEqual(digitFactorialChains(100000), 42);
2020-11-27 18:02:05 +00:00
```
2018-09-30 22:01:58 +00:00
2021-05-14 06:18:22 +00:00
`digitFactorialChains(500000)` should return `282` .
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
2021-05-14 06:18:22 +00:00
assert.strictEqual(digitFactorialChains(500000), 282);
```
`digitFactorialChains(1000000)` should return `402` .
```js
assert.strictEqual(digitFactorialChains(1000000), 402);
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-05-14 06:18:22 +00:00
function digitFactorialChains(n) {
2020-09-15 16:57:40 +00:00
2018-09-30 22:01:58 +00:00
return true;
}
2021-05-14 06:18:22 +00:00
digitFactorialChains(2000);
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-05-14 06:18:22 +00:00
function digitFactorialChains(n) {
function sumDigitsFactorials(number) {
let sum = 0;
while (number > 0) {
sum += factorials[number % 10];
number = Math.floor(number / 10);
}
return sum;
}
const factorials = [1];
for (let i = 1; i < 10 ; i + + ) {
factorials.push(factorials[factorials.length - 1] * i);
}
const sequences = {
169: 3,
871: 2,
872: 2,
1454: 3,
45362: 2,
45461: 2,
3693601: 3
};
let result = 0;
for (let i = 2; i < n ; i + + ) {
let curNum = i;
let chainLength = 0;
const curSequence = [];
while (curSequence.indexOf(curNum) === -1) {
curSequence.push(curNum);
curNum = sumDigitsFactorials(curNum);
chainLength++;
if (sequences.hasOwnProperty(curNum) > 0) {
chainLength += sequences[curNum];
break;
}
}
if (chainLength === 60) {
result++;
}
for (let j = 1; j < curSequence.length ; j + + ) {
sequences[curSequence[j]] = chainLength - j;
}
}
return result;
}
2018-09-30 22:01:58 +00:00
```