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:
@ -8,22 +8,46 @@ dashedName: problem-56-powerful-digit-sum
|
||||
|
||||
# --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` < 100, what is the maximum digital sum?
|
||||
Considering natural numbers of the form, $a^b$, where `a`, `b` < `n`, what is the maximum digital sum?
|
||||
|
||||
# --hints--
|
||||
|
||||
`powerfulDigitSum()` should return a number.
|
||||
`powerfulDigitSum(3)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof powerfulDigitSum() === 'number');
|
||||
assert(typeof powerfulDigitSum(3) === 'number');
|
||||
```
|
||||
|
||||
`powerfulDigitSum()` should return 972.
|
||||
`powerfulDigitSum(3)` should return `4`.
|
||||
|
||||
```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--
|
||||
@ -31,16 +55,47 @@ assert.strictEqual(powerfulDigitSum(), 972);
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function powerfulDigitSum() {
|
||||
function powerfulDigitSum(n) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
powerfulDigitSum();
|
||||
powerfulDigitSum(3);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```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;
|
||||
}
|
||||
```
|
||||
|
Reference in New Issue
Block a user