feat(tests): add euler-206 solution (#46256)
parent
b8cedbcc65
commit
78880fea1b
|
@ -34,5 +34,30 @@ concealedSquare();
|
|||
# --solutions--
|
||||
|
||||
```js
|
||||
// solution required
|
||||
// Check if n**2 matches the pattern
|
||||
function squareMatchs(n) {
|
||||
// Need BigInt due to size of values
|
||||
let nSquared = (BigInt(n) * BigInt(n)).toString();
|
||||
|
||||
// Check if digits match pattern
|
||||
for (let i = 1; i <= 9; i++) {
|
||||
if (nSquared[2 * (i - 1)] != i) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Find integer whose square matches the pattern
|
||||
function concealedSquare() {
|
||||
// Set bounds based upon max and min candidates
|
||||
const minSquareRoot = Math.floor(Math.sqrt(10203040506070809) / 10) * 10;
|
||||
const maxSquareRoot = Math.ceil(Math.sqrt(19293949596979899) / 10) * 10;
|
||||
|
||||
for (let x = maxSquareRoot; x >= minSquareRoot; x -= 10) {
|
||||
// Note: 3*3 = 9 and 7*7 = 49 are only trailing digits
|
||||
// that can produce 9 as trailing digit in square
|
||||
if (squareMatchs(x + 3)) return (x + 3)*10;
|
||||
if (squareMatchs(x + 7)) return (x + 7)*10;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
```
|
||||
|
|
Loading…
Reference in New Issue