Competitive programming often involves computation of Least Common Multiple (LCM) of two numbers. One way of doing that is using boost::math::lcm(), which we discussed in the post – Inbuilt function for calculating LCM in C++ .
But, recently, C++ in its latest version C++17 has also included another in-built function for computation of LCM, std::lcm(). This function is defined inside the header file .
Syntax:
std::lcm (m, n) Arguments: m, n Returns: 0, if either of m or n are 0 else, returns lcm of mod(m) and mod(n)
Remember, since this feature has been defined in latest version of C++, so using this function in compilers not supporting C++17, will throw an error.
// CPP program to illustrate // std::lcm function of C++ #include <iostream> #include <numeric> using namespace std; int main() { cout << "LCM(10, 20) = " << std::lcm(10, 20) << endl; return 0; } |
Output:
20
Important Points:
- This function works on positive numbers, and if any argument is negative, it is firstly converted to its modulus, and then calculates the LCM.
- Also, it works only on integer data type , and if any other data type like char, double, is provided in its argument, then it will throw an error.
Reference:
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.
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.
Recommended Posts:
- Custom Jumble Word Game
- Comparator Class in C++ with Examples
- Complete Guide to Install C++17 in Windows
- Features of C++
- Minimum replacement of pairs by their LCM required to reduce given array to its LCM
- Generate a pair of integers from a range [L, R] whose LCM also lies within the range
- Draw a triangle in C++ graphics
- Implementation of lower_bound() and upper_bound() in List of Pairs in C++
- How to Setup VSCode with C, C++ and Python for Competitive Programming
- Java System.exit(0) vs C++ return 0
- Features of C++17 with Examples
- Life cycle of Objects in C++ with Example
- Difference between virtual function and inline function in C++
- Difference between Turbo C++ and Dev C++
- Ellipsis in C++ with Examples