Algorithm/Programmers

[Programmers] LV2. 멀쩡한 사각형

Bogass 2023. 3. 15. 13:30
// LV2. 멀쩡한 사각형
public static long solution_normalSquare(long w, long h) {
    long answer = w * h; // 첫 사각형 크기

    long gcd = 1, idx = w;
    while (1 < idx) { // 공약수 구하기
        if (w % idx == 0 && h % idx == 0) {
            gcd *= idx;
            w /= idx;
            h /= idx;
            idx = w;
        } else idx--;
    }
    long gcd2 = BigInteger.valueOf(w).gcd(BigInteger.valueOf(h)).longValue();
    return answer - gcd * (w + h - 1);
}