function squaresInRectangle(w, h, n, roundDownSide = false) {
let sx, sy;
const px = Math.ceil(Math.sqrt((n * w) / h));
if (Math.floor((px * h) / w) * px < n) {
sx = h / Math.ceil((px * h) / w);
} else {
sx = w / px;
}
const py = Math.ceil(Math.sqrt((n * h) / w));
if (Math.floor((py * w) / h) * py < n) {
sy = w / Math.ceil((w * py) / h);
} else {
sy = h / py;
}
let side = Math.max(sx, sy);
if (roundDownSide) {
side = Math.floor(side);
}
const rows = Math.floor(h / side);
const cols = Math.floor(w / side);
const count = rows * cols;
return {
side,
rows,
cols,
count
};
}