chore(i18n,chn): manually downloaded curriculum (#42858)

This commit is contained in:
Mrugesh Mohapatra
2021-07-15 13:04:11 +05:30
committed by GitHub
parent eef1805fe6
commit 7eb0630f2d
248 changed files with 5645 additions and 2149 deletions

View File

@ -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;
}
```