mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-02-20 16:00:35 -05:00
1.4 KiB
1.4 KiB
id, title, challengeType, forumTopicId, dashedName
| id | title | challengeType | forumTopicId | dashedName |
|---|---|---|---|---|
| 5900f43a1000cf542c50ff4d | Problem 206: Concealed Square | 1 | 301847 | problem-206-concealed-square |
--description--
Find the unique positive integer whose square has the form 1_2_3_4_5_6_7_8_9_0, where each "_" is a single digit.
--hints--
concealedSquare() should return 1389019170.
assert.strictEqual(concealedSquare(), 1389019170);
--seed--
--seed-contents--
function concealedSquare() {
return true;
}
concealedSquare();
--solutions--
// 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;
}