유클리드 호제법 (Euclidean Algorithm)
유클리드 호제법은 두 정수의 최대 공약수(Greatest Common Divisor, GCD)를 효율적으로 찾는 알고리즘
public class Main {
public static void main(String[] args) {
// 두 수를 정의합니다.
int num1 = 48;
int num2 = 18;
// 최대 공약수를 계산합니다.
int gcd = gcd(num1, num2);
// 최소 공배수를 계산합니다.
int lcm = lcm(num1, num2, gcd);
// 결과를 출력합니다.
System.out.println("최대 공약수: " + gcd);
System.out.println("최소 공배수: " + lcm);
}
// 최대 공약수를 구하는 메서드
private static int gcd(int a, int b) {
while (b != 0) {
int temp = a % b;
a = b;
b = temp;
}
return a;
}
// 최소 공배수를 구하는 메서드
private static int lcm(int a, int b, int gcd) {
return (a * b) / gcd;
}
}