Main.java
package src;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
int valueLCM = 0;
ArrayList<Integer> list = new ArrayList<>();
list.add(4);
list.add(6);
list.add(9);
for(int index=0; index<list.size()-1; index++) {
if( index==0 ) {
valueLCM = calcLCM( list.get(index), list.get(index+1) );
} else {
valueLCM = calcLCM( valueLCM, list.get(index+1) );
}
}
System.out.println(valueLCM);
}
private static int calcLCM(int val1, int val2) {
int maxValue = Math.max(val1, val2);
int minValue = Math.min(val1, val2);
long val3 = maxValue * minValue;
if(minValue==0) return maxValue;
int temp;
while( (temp=maxValue%minValue)!=0 ) {
maxValue=minValue;
minValue=temp;
}
return (int)(val3/minValue);
}
}
After finding the greatest common divisor by Euclidean algorithm, Find the least common multiple.
Recommended Posts