fix(curriculum): rework Project Euler 56 (#42364)

* fix: rework challenge to use argument in function

* fix: add solution

* fix: use MathJax to improve look of math notation
This commit is contained in:
gikf
2021-06-11 20:22:54 +02:00
committed by GitHub
parent ea7683ac36
commit f0375198df

View File

@ -8,22 +8,46 @@ dashedName: problem-56-powerful-digit-sum
# --description-- # --description--
A googol (10<sup>100</sup>) is a massive number: one followed by one-hundred zeros; 100<sup>100</sup> is almost unimaginably large: one followed by two-hundred zeros. Despite their size, the sum of the digits in each number is only 1. A googol ($10^{100}$) is a massive number: one followed by one-hundred zeros; $100^{100}$ is almost unimaginably large: one followed by two-hundred zeros. Despite their size, the sum of the digits in each number is only 1.
Considering natural numbers of the form, `ab`, where `a`, `b` &lt; 100, what is the maximum digital sum? Considering natural numbers of the form, $a^b$, where `a`, `b` &lt; `n`, what is the maximum digital sum?
# --hints-- # --hints--
`powerfulDigitSum()` should return a number. `powerfulDigitSum(3)` should return a number.
```js ```js
assert(typeof powerfulDigitSum() === 'number'); assert(typeof powerfulDigitSum(3) === 'number');
``` ```
`powerfulDigitSum()` should return 972. `powerfulDigitSum(3)` should return `4`.
```js ```js
assert.strictEqual(powerfulDigitSum(), 972); assert.strictEqual(powerfulDigitSum(3), 4);
```
`powerfulDigitSum(10)` should return `45`.
```js
assert.strictEqual(powerfulDigitSum(10), 45);
```
`powerfulDigitSum(50)` should return `406`.
```js
assert.strictEqual(powerfulDigitSum(50), 406);
```
`powerfulDigitSum(75)` should return `684`.
```js
assert.strictEqual(powerfulDigitSum(75), 684);
```
`powerfulDigitSum(100)` should return `972`.
```js
assert.strictEqual(powerfulDigitSum(100), 972);
``` ```
# --seed-- # --seed--
@ -31,16 +55,47 @@ assert.strictEqual(powerfulDigitSum(), 972);
## --seed-contents-- ## --seed-contents--
```js ```js
function powerfulDigitSum() { function powerfulDigitSum(n) {
return true; return true;
} }
powerfulDigitSum(); powerfulDigitSum(3);
``` ```
# --solutions-- # --solutions--
```js ```js
// solution required function powerfulDigitSum(n) {
function sumDigitsOfPower(numA, numB) {
let digitsSum = 0;
let number = power(numA, numB);
while (number > 0n) {
const digit = number % 10n;
digitsSum += parseInt(digit, 10);
number = number / 10n;
}
return digitsSum;
}
function power(numA, numB) {
let sum = 1n;
for (let b = 0; b < numB; b++) {
sum = sum * BigInt(numA);
}
return sum;
}
const limit = n - 1;
let maxDigitsSum = 0;
for (let a = limit; a > 0; a--) {
for (let b = limit; b > 0; b--) {
const curDigitSum = sumDigitsOfPower(a, b);
if (curDigitSum > maxDigitsSum) {
maxDigitsSum = curDigitSum;
}
}
}
return maxDigitsSum;
}
``` ```