fix: rework Project Euler - Problem 57 (#40926)

* fix: rework challenge to use argument in function

* fix: add solution

* fix: correct variable name
This commit is contained in:
gikf
2021-02-17 20:58:18 +01:00
committed by GitHub
parent 1fe1371b16
commit 762775889e

View File

@ -24,20 +24,32 @@ $1 + \\frac 1 {2 + \\frac 1 {2+\\frac 1 {2+\\frac 1 2}}} = \\frac {41}{29} = 1.4
The next three expansions are $\\frac {99}{70}$, $\\frac {239}{169}$, and $\\frac {577}{408}$, but the eighth expansion, $\\frac {1393}{985}$, is the first example where the number of digits in the numerator exceeds the number of digits in the denominator. The next three expansions are $\\frac {99}{70}$, $\\frac {239}{169}$, and $\\frac {577}{408}$, but the eighth expansion, $\\frac {1393}{985}$, is the first example where the number of digits in the numerator exceeds the number of digits in the denominator.
In the first one-thousand expansions, how many fractions contain a numerator with more digits than denominator? In the first `n` expansions, how many fractions contain a numerator with more digits than denominator?
# --hints-- # --hints--
`squareRootConvergents()` should return a number. `squareRootConvergents(10)` should return a number.
```js ```js
assert(typeof squareRootConvergents() === 'number'); assert(typeof squareRootConvergents(10) === 'number');
``` ```
`squareRootConvergents()` should return 153. `squareRootConvergents(10)` should return 1.
```js ```js
assert.strictEqual(squareRootConvergents(), 153); assert.strictEqual(squareRootConvergents(10), 1);
```
`squareRootConvergents(100)` should return 15.
```js
assert.strictEqual(squareRootConvergents(100), 15);
```
`squareRootConvergents(1000)` should return 153.
```js
assert.strictEqual(squareRootConvergents(1000), 153);
``` ```
# --seed-- # --seed--
@ -45,16 +57,42 @@ assert.strictEqual(squareRootConvergents(), 153);
## --seed-contents-- ## --seed-contents--
```js ```js
function squareRootConvergents() { function squareRootConvergents(n) {
return true; return true;
} }
squareRootConvergents(); squareRootConvergents(1000);
``` ```
# --solutions-- # --solutions--
```js ```js
// solution required function squareRootConvergents(n) {
function countDigits(number) {
let counter = 0;
while (number > 0) {
counter++;
number = number / 10n;
}
return counter;
}
// Use BigInt as integer won't handle all cases
let numerator = 3n;
let denominator = 2n;
let moreDigitsInNumerator = 0;
for (let i = 2; i <= n; i++) {
[numerator, denominator] = [
numerator + 2n * denominator,
denominator + numerator
];
if (countDigits(numerator) > countDigits(denominator)) {
moreDigitsInNumerator++;
}
}
return moreDigitsInNumerator;
}
``` ```