Print
카테고리: [ Development ]
조회수: 1206

1. 최소공배수


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));
  }
}