LCM of two Numbers in Java

Overview
In mathematical terms, the smallest possible integer that is divisible by two (or more) numbers is known as the Least Common Multiple (LCM) of the numbers. We will be discussing various methods to calculate the LCM of the numbers.
Introduction
The Least Common Multiple (LCM) of two or more numbers is the smallest positive number that can be divided by the numbers without leaving a remainder. LCM is also known by the terms: Lowest Common Multiple (LCM), Least Common Denominator, and Smallest Common Multiple. LCM (a, b) or lcm (a, b) are the symbols for it, where a and b are two numbers.
For example: LCM of , and is . Since is the smallest integer that is completely divisible by all three numbers.
Properties of LCM
LCM is associative, commutative and distributive, which means:
- Associative: LCM (a, b) = LCM (b, a)
- Commutative: LCM (a, b, c) = LCM (LCM (a, b), c) = LCM (a, LCM (b, c))
- Distributive: LCM (ka, kb, kc) = kLCM (a, b, c)
Now, that we know about LCM and its properties, let's now look at the different ways to find the LCM of two numbers.
LCM Using Prime Factorization Method
Our first approach involves finding the prime factors of each number and then calculating the LCM using those, the same way we used to do it in our earlier days at school. Below is the recursive code for the method:
Output:
LCM Using While loop and if Statement
The next method is by using the while loop and if statement. The method is pretty straightforward. Let's dive into the code and understand how it works:
Output:
In the above code, we have initialized the LCM to the greater number and kept incrementing it, until a number is found which is divisible by both numbers.
LCM Using Multiples of Numbers
The method of finding LCM using multiples of numbers is probably the least used approach. This is mainly because the method is very lengthy. This approach is carried out in two steps:
- List each number's multiples until the first common multiple is discovered.
- Choose the smallest multiple that all of the numbers have in common.
That's about it. Now, let's code it out:
Output:
Calculate LCM using GCD
Before discussing the method of calculating LCM using GCD, we should understand what GCD is: Greatest Common Divisor: It's the largest number that splits two or more numbers fully. It's also known as the Highest Common Factor (HCF) or the Greatest Common Factor (GCF).
There is also a relationship between GCD and LCM:
Using the above relation, we can easily find the LCM using the GCD. We can now look at the code for the same:
Output:
Note: We can further optimize the time complexity of the code by improvising the time complexity to calculate GCD. To learn more about it, you can refer to GCD of Two Numbers in Java
Conclusion
Finally, let's summarise everything we have learned in the article:
- The smallest positive integer that is perfectly divisible by two numbers, a and b is the Least Common Multiple (LCM) of two non-zero integers (a, b).
- LCM is associative, commutative and distributive.
- LCM of two numbers can be calculated using the methods of prime factorization, while loop and if statement, multiples of numbers, and the GCD of the two numbers.