fix(curriculum): rework Project Euler 80 (#42128)
* fix: rework challenge to use argument in function * fix: add solution
This commit is contained in:
@ -10,22 +10,34 @@ dashedName: problem-80-square-root-digital-expansion
|
||||
|
||||
It is well known that if the square root of a natural number is not an integer, then it is irrational. The decimal expansion of such square roots is infinite without any repeating pattern at all.
|
||||
|
||||
The square root of two is 1.41421356237309504880..., and the digital sum of the first one hundred decimal digits is 475.
|
||||
The square root of two is `1.41421356237309504880...`, and the digital sum of the first one hundred decimal digits is `475`.
|
||||
|
||||
For the first one hundred natural numbers, find the total of the digital sums of the first one hundred decimal digits for all the irrational square roots.
|
||||
For the first `n` natural numbers, find the total of the digital sums of the first one hundred decimal digits for all the irrational square roots.
|
||||
|
||||
# --hints--
|
||||
|
||||
`sqrtDigitalExpansion()` should return a number.
|
||||
`sqrtDigitalExpansion(2)` should return a number.
|
||||
|
||||
```js
|
||||
assert(typeof sqrtDigitalExpansion() === 'number');
|
||||
assert(typeof sqrtDigitalExpansion(2) === 'number');
|
||||
```
|
||||
|
||||
`sqrtDigitalExpansion()` should return 40886.
|
||||
`sqrtDigitalExpansion(2)` should return `475`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(sqrtDigitalExpansion(), 40886);
|
||||
assert.strictEqual(sqrtDigitalExpansion(2), 475);
|
||||
```
|
||||
|
||||
`sqrtDigitalExpansion(50)` should return `19543`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(sqrtDigitalExpansion(50), 19543);
|
||||
```
|
||||
|
||||
`sqrtDigitalExpansion(100)` should return `40886`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(sqrtDigitalExpansion(100), 40886);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
@ -33,16 +45,65 @@ assert.strictEqual(sqrtDigitalExpansion(), 40886);
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function sqrtDigitalExpansion() {
|
||||
function sqrtDigitalExpansion(n) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
sqrtDigitalExpansion();
|
||||
sqrtDigitalExpansion(2);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
function sqrtDigitalExpansion(n) {
|
||||
function sumDigits(number) {
|
||||
let sum = 0;
|
||||
while (number > 0n) {
|
||||
let digit = number % 10n;
|
||||
sum += parseInt(digit, 10);
|
||||
number = number / 10n;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
function power(numberA, numberB) {
|
||||
let result = 1n;
|
||||
for (let b = 0; b < numberB; b++) {
|
||||
result = result * BigInt(numberA);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Based on http://www.afjarvis.staff.shef.ac.uk/maths/jarvisspec02.pdf
|
||||
function expandSquareRoot(number, numDigits) {
|
||||
let a = 5n * BigInt(number);
|
||||
let b = 5n;
|
||||
const boundaryWithNeededDigits = power(10, numDigits + 1);
|
||||
|
||||
while (b < boundaryWithNeededDigits) {
|
||||
if (a >= b) {
|
||||
a = a - b;
|
||||
b = b + 10n;
|
||||
} else {
|
||||
a = a * 100n;
|
||||
b = (b / 10n) * 100n + 5n;
|
||||
}
|
||||
}
|
||||
return b / 100n;
|
||||
}
|
||||
|
||||
let result = 0;
|
||||
let nextPerfectRoot = 1;
|
||||
const requiredDigits = 100;
|
||||
for (let i = 1; i <= n; i++) {
|
||||
if (nextPerfectRoot ** 2 === i) {
|
||||
nextPerfectRoot++;
|
||||
continue;
|
||||
}
|
||||
result += sumDigits(expandSquareRoot(i, requiredDigits));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
```
|
||||
|
Reference in New Issue
Block a user