From a7556a82d55666210e84fc347ec0f7669a154128 Mon Sep 17 00:00:00 2001 From: gikf <60067306+gikf@users.noreply.github.com> Date: Mon, 31 May 2021 22:22:05 +0200 Subject: [PATCH] fix(curriculum): rework Project Euler 95 (#42255) --- .../problem-95-amicable-chains.md | 92 ++++++++++++++++--- 1 file changed, 81 insertions(+), 11 deletions(-) diff --git a/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-95-amicable-chains.md b/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-95-amicable-chains.md index 037330b660..9d42e7c6f5 100644 --- a/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-95-amicable-chains.md +++ b/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-95-amicable-chains.md @@ -14,26 +14,44 @@ 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: -
- 12496 → 14288 → 15472 → 14536 → 14264 (→ 12496 → ...) -
+$$ + 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 +59,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; +} ```