-
Tech Note 정보
-
강철지그 님이 작성하신 글입니다.
-
카테고리: [ Development ]
-
-
-
-
조회수: 1954
1. 최소공배수
- Least Common Multiple
- 공배수 중 가장 작은 수
2. 구현
public class LeastCommonMultiple {
long get(int[] num) {
long min = 0;
long max = 0;
long result = num[0];
int thisNum = 0;
for (int inx = 1; inx < num.length; inx++) {
thisNum = num[inx];
max = Math.max(result, thisNum);
min = Math.min(result, thisNum);
result = max * min / gcd(max, min);
}
return result;
}
long gcd(long i, long j) {
if (i > j) {
if (j == 0)
return i;
else
return gcd(j, i % j);
} else {
return gcd(j, i);
}
}
public static void main(String[] args) {
LeastCommonMultiple lcm = new LeastCommonMultiple();
int[] ex = { 3, 7, 9, 28 };
System.out.println("result=" + lcm.get(ex));
}
}