chore: manual translations (#42811)
This commit is contained in:
committed by
GitHub
parent
a3395269a0
commit
c4fd49e5b7
@ -14,26 +14,42 @@ Interestingly the sum of the proper divisors of 220 is 284 and the sum of the pr
|
||||
|
||||
Perhaps less well known are longer chains. For example, starting with 12496, we form a chain of five numbers:
|
||||
|
||||
<div style="text-align: center;">
|
||||
12496 → 14288 → 15472 → 14536 → 14264 (→ 12496 → ...)
|
||||
</div>
|
||||
$$ 12496 → 14288 → 15472 → 14536 → 14264 \\,(→ 12496 → \cdots) $$
|
||||
|
||||
Since this chain returns to its starting point, it is called an amicable chain.
|
||||
|
||||
Find the smallest member of the longest amicable chain with no element exceeding one million.
|
||||
Find the smallest member of the longest amicable chain with no element exceeding `limit`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`amicableChains()` should return a number.
|
||||
`amicableChains(300)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof amicableChains() === 'number');
|
||||
assert(typeof amicableChains(300) === 'number');
|
||||
```
|
||||
|
||||
`amicableChains()` should return 14316.
|
||||
`amicableChains(300)` should return `220`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(amicableChains(), 14316);
|
||||
assert.strictEqual(amicableChains(300), 220);
|
||||
```
|
||||
|
||||
`amicableChains(15000)` should return `220`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(amicableChains(15000), 220);
|
||||
```
|
||||
|
||||
`amicableChains(100000)` should return `12496`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(amicableChains(100000), 12496);
|
||||
```
|
||||
|
||||
`amicableChains(1000000)` should return `14316`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(amicableChains(1000000), 14316);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
@ -41,16 +57,68 @@ assert.strictEqual(amicableChains(), 14316);
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function amicableChains() {
|
||||
function amicableChains(limit) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
amicableChains();
|
||||
amicableChains(300);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
function amicableChains(limit) {
|
||||
function getSmallestMember(chain) {
|
||||
let smallest = chain[0];
|
||||
for (let i = 1; i < chain.length; i++) {
|
||||
if (smallest > chain[i]) {
|
||||
smallest = chain[i];
|
||||
}
|
||||
}
|
||||
return smallest;
|
||||
}
|
||||
|
||||
function getFactorsSums(limit) {
|
||||
const factorsSums = new Array(limit + 1).fill(1);
|
||||
for (let i = 2; i <= limit / 2; i++) {
|
||||
for (let j = 2 * i; j <= limit; j += i) {
|
||||
factorsSums[j] += i;
|
||||
}
|
||||
}
|
||||
return factorsSums;
|
||||
}
|
||||
|
||||
const factorsSums = getFactorsSums(limit);
|
||||
const checkedNumbers = new Set();
|
||||
|
||||
let longestChain = 0;
|
||||
let smallestMember = 0;
|
||||
for (let i = 0; i <= limit; i++) {
|
||||
const curChain = [];
|
||||
let curNumber = i;
|
||||
while (!checkedNumbers.has(curNumber) && factorsSums[curNumber] <= limit) {
|
||||
curNumber = factorsSums[curNumber];
|
||||
|
||||
const chainStart = curChain.indexOf(curNumber);
|
||||
if (chainStart === -1) {
|
||||
curChain.push(curNumber);
|
||||
continue;
|
||||
}
|
||||
|
||||
const chainLength = curChain.length - chainStart;
|
||||
if (chainLength > longestChain) {
|
||||
longestChain = chainLength;
|
||||
smallestMember = getSmallestMember(curChain.slice(chainStart));
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
for (let j = 0; j < curChain.length; j++) {
|
||||
checkedNumbers.add(curChain[j]);
|
||||
}
|
||||
}
|
||||
|
||||
return smallestMember;
|
||||
}
|
||||
```
|
||||
|
Reference in New Issue
Block a user