* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
1.7 KiB
1.7 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
5900f3811000cf542c50fe94 | Problem 21: Amicable numbers | 5 | 301851 | problem-21-amicable-numbers |
--description--
Let d(n
) be defined as the sum of proper divisors of n
(numbers less than n
which divide evenly into n
).
If d(a
) = b
and d(b
) = a
, where a
≠ b
, then a
and b
are an amicable pair and each of a
and b
are called amicable numbers.
For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.
Evaluate the sum of all the amicable numbers under n
.
--hints--
sumAmicableNum(1000)
should return a number.
assert(typeof sumAmicableNum(1000) === 'number');
sumAmicableNum(1000)
should return 504.
assert.strictEqual(sumAmicableNum(1000), 504);
sumAmicableNum(2000)
should return 2898.
assert.strictEqual(sumAmicableNum(2000), 2898);
sumAmicableNum(5000)
should return 8442.
assert.strictEqual(sumAmicableNum(5000), 8442);
sumAmicableNum(10000)
should return 31626.
assert.strictEqual(sumAmicableNum(10000), 31626);
--seed--
--seed-contents--
function sumAmicableNum(n) {
return n;
}
sumAmicableNum(10000);
--solutions--
const sumAmicableNum = (n) => {
const fsum = (n) => {
let sum = 1;
for (let i = 2; i <= Math.floor(Math.sqrt(n)); i++)
if (Math.floor(n % i) === 0)
sum += i + Math.floor(n / i);
return sum;
};
let d = [];
let amicableSum = 0;
for (let i=2; i<n; i++) d[i] = fsum(i);
for (let i=2; i<n; i++) {
let dsum = d[i];
if (d[dsum]===i && i!==dsum) amicableSum += i+dsum;
}
return amicableSum/2;
};