231125 N개의 최소공배수

Jongleee·2023년 11월 25일
0

TIL

목록 보기
426/576
public int solution(int[] arr) {
	int answer = arr[0];
	for (int i = 1; i < arr.length; i++) {
		answer = lcm(answer, arr[i]);
	}
	return answer;
}

public int lcm(int x, int y) {
	return x * y / gcd(x, y);
}

public int gcd(int x, int y) {
	int a = Math.max(x, y);
	int b = Math.min(x, y);
	while (a % b != 0) {
		int r = a % b;
		a = b;
		b = r;
	}
	return b;
}

출처:https://school.programmers.co.kr/learn/courses/30/lessons/12953

0개의 댓글