LCM (Least Common Multiple) of two numbers is the smallest number which can be divided by both numbers.
For example LCM of 15 and 20 is 60 and LCM of 5 and 7 is 35.
A simple solution is to find all prime factors of both numbers, then find union of all factors present in both numbers. Finally return product of elements in union.
An efficient solution is based on below formula for LCM of two numbers ‘a’ and ‘b’.
a x b = LCM(a, b) * GCD (a, b) LCM(a, b) = (a x b) / GCD(a, b)
We have discussed function to find GCD of two numbers. Using GCD, we can find LCM.
Below is the implementation of above idea :
LCM of 15 and 20 is 60
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Improved By : vt_m
- Program to find GCD or HCF of two numbers
- LCM of given array elements
- GCD of more than two (or array) numbers
- Number Theory (Interesting Facts and Algorithms)
- Program for factorial of a number
- Number of Permutations such that no Three Terms forms Increasing Subsequence
- Check if the first and last digit of the smallest number forms a prime
- Print all substring of a number without any conversion
- Complement of a number with any base b
- Check if Decimal representation of an Octal number is divisible by 7