L.C.M. या दो मानों का लघुत्तम समापवर्त्य, वह सबसे छोटा धनात्मक मान है जो दोनों मानों का गुणक है।
उदाहरण के लिए 3 और 4 के गुणक हैं:
3 → 3, 6, 9, 12, 15 ...
4 → 4, 8, 12, 16, 20 ...
दोनों का सबसे छोटा सामान्य गुणक 12 है, इसलिए 3 और 4 का LCM 12 है।
प्रोग्राम
निम्न उदाहरण संख्याओं की सरणी का LCM गणना करता है।
public class LCMofArrayOfNumbers {
public static void main(String args[]) {
int[] myArray = {25, 50, 125, 625};
int min, max, x, lcm = 0;
for(int i = 0; i < myArray.length; i++) {
for(int j = i+1; j < myArray.length-1; j++) {
if(myArray[i] > myArray[j]) {
min = myArray[j];
max = myArray[i];
} else {
min = myArray[i];
max = myArray[j];
}
for(int k =0; k < myArray.length; k++) {
x = k * max;
if(x % min == 0) {
lcm = x;
}
}
}
}
System.out.println("LCM of the given array of numbers : " + lcm);
}
}
आउटपुट
LCM of the given array of numbers : 250
नोट: दिया गया एल्गोरिदम अकुशल है; बेहतर तरीका यूक्लिडियन एल्गोरिदम का उपयोग करके GCD निकालना और फिर LCM = (a*b)/GCD सूत्र का उपयोग करना है।