본문 바로가기

Study/Java

[Java] 최대공약수와 최소공배수를 구하는 함수

 

package com;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int t = Integer.parseInt(br.readLine());

        StringTokenizer st;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < t; i++) {
            st = new StringTokenizer(br.readLine());
            int p = Integer.parseInt(st.nextToken());
            int q = Integer.parseInt(st.nextToken());

            int res = getLcm(p, q);
            sb.append(res).append("\n");
        }

        System.out.println(sb);
    }

    // 최대공약수
    public static int getGcd(int p, int q) {
        if (p % q == 0) {
            return q;
        }
        return getGcd(q, p % q);
    }

    // 최소공배수
    public static int getLcm(int p, int q) {
        return p * q / getGcd(p, q);
    }
}