feat(tests): add euler-206 solution (#46256)

pull/46255/merge
Jeremy L Thompson 2022-05-31 13:27:02 -06:00 committed by GitHub
parent b8cedbcc65
commit 78880fea1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 26 additions and 1 deletions

View File

@ -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;
}
```