Find LCM of Multiple Numbers in Java
Problem
The LCM of a whole group of numbers is the smallest number that every one of them divides into evenly — it can be built up by combining the LCM of just two numbers at a time.
Given an array of integers, find the least common multiple shared by all of them.
Java Program
public class LCMOfMultipleNumbers {
static int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
static int lcm(int a, int b) {
return (a * b) / gcd(a, b); // lcm(a, b) * gcd(a, b) = a * b, solved for the lcm
}
public static void main(String[] args) {
int[] nums = {4, 6, 8};
int result = nums[0];
for (int i = 1; i < nums.length; i++) {
result = lcm(result, nums[i]); // fold the next number into the running LCM
}
System.out.println("LCM: " + result);
}
}Output
Core Logic
The LCM of a group of numbers is the same as folding the two-number LCM across the whole array, one element at a time — lcm(lcm(a, b), c) equals lcm(a, b, c).
- 1
resultstarts out holding the first element of the array. - 2A loop visits every remaining element, replacing
resultwithlcm(result, nums[i])at each step. - 3
lcm(a, b)itself is computed via(a * b) / gcd(a, b), reusing the same GCD-based formula as the two-number version. - 4After the last element has been folded in,
resultholds the LCM of the entire array.
[4, 6, 8], lcm(4, 6) is 12, and lcm(12, 8) is 24 — the LCM shared by all three.Key Point: This works because LCM, like GCD, is associative — combining numbers two at a time, in any order, always arrives at the same final answer.
Why: lcm() is called once per remaining element, and each call's cost is dominated by its internal gcd() call, so the total scales with both the array's length and that per-call cost.
Key Concepts
Approach 2: Java 8
import java.util.Arrays;
public class LCMOfMultipleNumbersStream {
static int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
static int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
public static void main(String[] args) {
int[] nums = {4, 6, 8};
// Folds lcm() across every element, the same way the manual loop does
int result = Arrays.stream(nums).reduce(LCMOfMultipleNumbersStream::lcm).getAsInt();
System.out.println("LCM: " + result);
}
}
Output
Core Logic
The same fold-two-at-a-time idea is exactly what Stream.reduce() is built for — no manual loop variable is needed.
- 1
Arrays.stream(nums)converts theint[]into anIntStream. - 2
.reduce(...)combines every element using the given operator, carrying the running result forward the same way the manual loop'sresultvariable does. - 3The method reference
::lcmsupplies the pairwise combining logic without writing a lambda body out longhand. - 4
.getAsInt()unwraps theOptionalIntthatreduce()without an identity value returns.
[4, 6, 8] with lcm folds 4 and 6 down to 12 first, then folds that 12 with 8, landing on the same answer, 24.Key Point: This is the exact same pattern used to find the GCD of multiple numbers with streams — only the combining function changes from gcd to lcm.
Why: reduce() still calls lcm() once per remaining element, the same total work as the manual loop, just expressed as a stream fold.