Inbuilt function for calculating LCM in C++

24 Jul, 2017
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>.
Syntax:
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).

// CPP program to illustrate// boost::math::lcm function of C++ #include <iostream>#include <boost/math/common_factor.hpp>  using namespace std;  int main(){    cout << "LCM(10,20) = " << boost::math::lcm(10,20)          << endl;    return 0;}
Output:
LCM(10,20) = 20

Important points:
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++ #include <iostream>#include <boost/math/common_factor.hpp>  using namespace std;  int main(){    cout << "LCM(1.0,20) = " << boost::math::lcm(1.0,20)          << endl;    return 0;}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.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 contribute@geeksforgeeks.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.My Personal Notes
arrow_drop_upSave
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++ #include <iostream>#include <boost/math/common_factor.hpp>  using namespace std;  int main(){    cout << "LCM(1.0,20) = " << boost::math::lcm(1.0,20)          << endl;    return 0;}This code will throw an error, as one of the argument of the function is a double type, so this code will not work.
// CPP program to illustrate illegal// behaviour of boost::math::lcm function of C++ #include <iostream>#include <boost/math/common_factor.hpp>  using namespace std;  int main(){    cout << "LCM(1.0,20) = " << boost::math::lcm(1.0,20)          << endl;    return 0;}
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.
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 contribute@geeksforgeeks.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.
LCM
C++
Mathematical
Mathematical
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Set in C++ Standard Template Library (STL)
Priority Queue in C++ Standard Template Library (STL)
vector erase() and clear() in C++
Substring in C++
unordered_map in C++ STL
Program for Fibonacci numbers
Set in C++ Standard Template Library (STL)
Write a program to print all permutations of a given string
C++ Data Types
Merge two sorted arrays