fix(curriculum): rework Project Euler 32 (#42342)
* fix: rework challenge to use argument in function * fix: change solution to allow using argument
This commit is contained in:
@ -12,22 +12,46 @@ We shall say that an `n`-digit number is pandigital if it makes use of all the d
|
|||||||
|
|
||||||
The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital.
|
The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital.
|
||||||
|
|
||||||
Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.
|
Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through `n` pandigital.
|
||||||
|
|
||||||
**Hint:** Some products can be obtained in more than one way so be sure to only include it once in your sum.
|
**Hint:** Some products can be obtained in more than one way so be sure to only include it once in your sum.
|
||||||
|
|
||||||
# --hints--
|
# --hints--
|
||||||
|
|
||||||
`pandigitalProducts()` should return a number.
|
`pandigitalProducts(4)` should return a number.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
assert(typeof pandigitalProducts() === 'number');
|
assert(typeof pandigitalProducts(4) === 'number');
|
||||||
```
|
```
|
||||||
|
|
||||||
`pandigitalProducts()` should return 45228.
|
`pandigitalProducts(4)` should return `12`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
assert.strictEqual(pandigitalProducts(), 45228);
|
assert.strictEqual(pandigitalProducts(4), 12);
|
||||||
|
```
|
||||||
|
|
||||||
|
`pandigitalProducts(6)` should return `162`.
|
||||||
|
|
||||||
|
```js
|
||||||
|
assert.strictEqual(pandigitalProducts(6), 162);
|
||||||
|
```
|
||||||
|
|
||||||
|
`pandigitalProducts(7)` should return `0`.
|
||||||
|
|
||||||
|
```js
|
||||||
|
assert.strictEqual(pandigitalProducts(7), 0);
|
||||||
|
```
|
||||||
|
|
||||||
|
`pandigitalProducts(8)` should return `13458`.
|
||||||
|
|
||||||
|
```js
|
||||||
|
assert.strictEqual(pandigitalProducts(8), 13458);
|
||||||
|
```
|
||||||
|
|
||||||
|
`pandigitalProducts(9)` should return `45228`.
|
||||||
|
|
||||||
|
```js
|
||||||
|
assert.strictEqual(pandigitalProducts(9), 45228);
|
||||||
```
|
```
|
||||||
|
|
||||||
# --seed--
|
# --seed--
|
||||||
@ -35,22 +59,21 @@ assert.strictEqual(pandigitalProducts(), 45228);
|
|||||||
## --seed-contents--
|
## --seed-contents--
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function pandigitalProducts() {
|
function pandigitalProducts(n) {
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
pandigitalProducts();
|
pandigitalProducts(4);
|
||||||
```
|
```
|
||||||
|
|
||||||
# --solutions--
|
# --solutions--
|
||||||
|
|
||||||
```js
|
```js
|
||||||
function pandigitalProducts() {
|
function pandigitalProducts(n) {
|
||||||
function is1to9Pandigital(...numbers) {
|
function is1toNPandigital(n, digitStr) {
|
||||||
const digitStr = concatenateNums(...numbers);
|
// check if length is n
|
||||||
// check if length is 9
|
if (digitStr.length !== n) {
|
||||||
if (digitStr.length !== 9) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// check if pandigital
|
// check if pandigital
|
||||||
@ -70,15 +93,24 @@ function pandigitalProducts() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const pandigitalNums = [];
|
const pandigitalNums = [];
|
||||||
|
const limit = 10 ** Math.floor(n / 2) - 1;
|
||||||
let sum = 0;
|
let sum = 0;
|
||||||
for (let mult1 = 2; mult1 < 9876; mult1++) {
|
for (let mult1 = 2; mult1 < limit; mult1++) {
|
||||||
let mult2 = 123;
|
for (let mult2 = 2; mult2 < limit; mult2++) {
|
||||||
while (concatenateNums(mult1, mult2, mult1 * mult2).length < 10) {
|
const product = mult1 * mult2;
|
||||||
if (is1to9Pandigital(mult1, mult2, mult1 * mult2) && !pandigitalNums.includes(mult1 * mult2)) {
|
const concatenated = concatenateNums(mult1, mult2, product);
|
||||||
pandigitalNums.push(mult1 * mult2);
|
if (concatenated.length > n) {
|
||||||
sum += mult1 * mult2;
|
break;
|
||||||
|
} else if (concatenated.length < n) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
is1toNPandigital(n, concatenated) &&
|
||||||
|
!pandigitalNums.includes(product)
|
||||||
|
) {
|
||||||
|
pandigitalNums.push(product);
|
||||||
|
sum += product;
|
||||||
}
|
}
|
||||||
mult2++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return sum;
|
return sum;
|
||||||
|
Reference in New Issue
Block a user