fix(learn): rework Project Euler 63 (#41425)
* fix: rework challenge to use argument in function * fix: add solution * fix: correct function call in seed code Co-authored-by: Tom <20648924+moT01@users.noreply.github.com> * fix: improve instructions wording * fix: correct typo Co-authored-by: Nicholas Carrigan (he/him) <nhcarrigan@gmail.com> Co-authored-by: Tom <20648924+moT01@users.noreply.github.com> Co-authored-by: Nicholas Carrigan (he/him) <nhcarrigan@gmail.com>
This commit is contained in:
@ -10,20 +10,74 @@ dashedName: problem-63-powerful-digit-counts
|
||||
|
||||
The 5-digit number, 16807 = 7<sup>5</sup>, is also a fifth power. Similarly, the 9-digit number, 134217728 = 8<sup>9</sup>, is a ninth power.
|
||||
|
||||
How many `n`-digit positive integers exist which are also an `n`th power?
|
||||
Complete the function so that it returns how many positive integers are of length `n` and an `n`th power.
|
||||
|
||||
# --hints--
|
||||
|
||||
`powerfulDigitCounts()` should return a number.
|
||||
`powerfulDigitCounts(1)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof powerfulDigitCounts() === 'number');
|
||||
assert(typeof powerfulDigitCounts(1) === 'number');
|
||||
```
|
||||
|
||||
`powerfulDigitCounts()` should return 49.
|
||||
`powerfulDigitCounts(1)` should return `9`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(powerfulDigitCounts(), 49);
|
||||
assert.strictEqual(powerfulDigitCounts(1), 9);
|
||||
```
|
||||
|
||||
`powerfulDigitCounts(2)` should return `6`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(powerfulDigitCounts(2), 6);
|
||||
```
|
||||
|
||||
`powerfulDigitCounts(3)` should return `5`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(powerfulDigitCounts(3), 5);
|
||||
```
|
||||
|
||||
`powerfulDigitCounts(4)` should return `4`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(powerfulDigitCounts(4), 4);
|
||||
```
|
||||
|
||||
`powerfulDigitCounts(5)` should return `3`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(powerfulDigitCounts(5), 3);
|
||||
```
|
||||
|
||||
`powerfulDigitCounts(6)` should return `3`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(powerfulDigitCounts(6), 3);
|
||||
```
|
||||
|
||||
`powerfulDigitCounts(7)` should return `2`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(powerfulDigitCounts(7), 2);
|
||||
```
|
||||
|
||||
`powerfulDigitCounts(8)` should return `2`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(powerfulDigitCounts(8), 2);
|
||||
```
|
||||
|
||||
`powerfulDigitCounts(10)` should return `2`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(powerfulDigitCounts(10), 2);
|
||||
```
|
||||
|
||||
`powerfulDigitCounts(21)` should return `1`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(powerfulDigitCounts(21), 1);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
@ -31,16 +85,38 @@ assert.strictEqual(powerfulDigitCounts(), 49);
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function powerfulDigitCounts() {
|
||||
function powerfulDigitCounts(n) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
powerfulDigitCounts();
|
||||
powerfulDigitCounts(1);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
function powerfulDigitCounts(n) {
|
||||
function countDigits(num) {
|
||||
let counter = 0;
|
||||
while (num > 0) {
|
||||
num = Math.floor(num / 10);
|
||||
counter++;
|
||||
}
|
||||
return counter;
|
||||
}
|
||||
|
||||
let numbersCount = 0;
|
||||
|
||||
let curNum = 1;
|
||||
while (curNum < 10) {
|
||||
let power = n;
|
||||
if (power === countDigits(curNum ** power)) {
|
||||
numbersCount++;
|
||||
}
|
||||
curNum++;
|
||||
}
|
||||
|
||||
return numbersCount;
|
||||
}
|
||||
```
|
||||
|
Reference in New Issue
Block a user