카펫은 노란 타일을 둘러싼 갈색 타일, 각각 타일의 수가 주어지면 전체 타일의 가로, 세로의 길이를 반환하는 문제이다.
function solution(brown, yellow) {
// 총 타일의 갯수
const total = brown + yellow
for (let i = Math.floor(Math.sqrt(total)); i >= 3; i--) {
// 내림한 타일의 제곱근부터 3까지 반복
if (total % i === 0) {
// 전체 갯수와 반복되는 수의 나머지가 0일 때
let width = total / i,
height = total / width;
if ((width - 2) * (height - 2) === yellow) return [width,height]
}
}
}