JSFiddle - React, Tailwind, and code Playground

by Jon-Carlos Rivera

JavaScript

function isEven(a) {
    return a % 2 === 0;
}

function gcd(a, b) {
    var d = 0;
    while (isEven(a) && isEven(b)) {
        a = a / 2;
        b = b / 2;
        d = d + 1;
    }
    while (a != b) {
        if (isEven(a)) {
            a = a / 2;
        } else if (isEven(b)) {
            b = b / 2;
        } else if (a > b) {
            a = (a - b) / 2;
        } else {
            b = (b - a) / 2;
        }
    }

    return Math.pow(2, d) * a;
}

function optimalSquareTiles(width, height) {
    var g = gcd(width, height);

    return {
        columns: width / g,
        rows: height / g,
        size: g
    };
}

console.log(optimalSquareTiles(320, 240));