Many times while we do programming, we need to calculate the Least Common Multiple (LCM) between two numbers. We have already discussed how to find LCM in this post.
In place of defining and then using a function for calculating lcm , we can simply use an inbuilt function of boost library of C++ , boost::math::lcm ().
For using this function , we have to declare a header file <boost/math/common_factor.hpp>.
boost::math::lcm (m,n) Parameters: m, n Return Value: 0 if either m or n are zero, else lcm of mod(m) and mod(n).
LCM(10,20) = 20
- The function will calculate the lcm after taking the modulus of both the numbers, so in case if any of the number being negative, it will be converted to its modulus and then LCM is calculated.
- In case if any of the number being a non-integer data type , then this function will throw an error.
// CPP program to illustrate illegal
// behaviour of boost::math::lcm function of C++
"LCM(1.0,20) = "
This code will throw an error, as one of the argument of the function is a double type, so this code will not work.
- In C++17, a new STL function for calculating LCM of two numbers, std::lcm(), has been introduced, which can be used on any compiler supporting C++17 features.
- Sum of LCM(1, n), LCM(2, n), LCM(3, n), ... , LCM(n, n)
- std::gcd | C++ inbuilt function for finding GCD
- Minimum replacement of pairs by their LCM required to reduce given array to its LCM
- Calculating Factorials using Stirling Approximation
- Calculating n-th real root using binary search
- LCM of given array elements
- LCM of First n Natural Numbers
- GCD, LCM and Distributive Property
- Prime factors of LCM of array elements
- Maximum sum of distinct numbers with LCM as N
- Finding LCM of more than two (or array) numbers without using GCD
- Program to find LCM of two numbers
- QA - Placement Quizzes | Numbers, LCM and HCF | Question 11
- QA - Placement Quizzes | Numbers, LCM and HCF | Question 12
- QA - Placement Quizzes | LCM | Question 3
- QA - Placement Quizzes | LCM | Question 4
- QA - Placement Quizzes | LCM | Question 5
- QA - Placement Quizzes | LCM | Question 6
- QA - Placement Quizzes | LCM | Question 7
- QA - Placement Quizzes | LCM | Question 8
This article is contributed by Mrigendra Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.