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 .
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.
- 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.
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.
- Program to implement Separate Chaining in C++ STL without the use of pointers
- Speed up Code executions with help of Pragma in C/C++
- Vector of Vectors in C++ STL with Examples
- Array of Vectors in C++ STL
- Minimum possible value of max(A, B) such that LCM(A, B) = C
- Difference between Virtual function and Pure virtual function in C++
- cin get() in C++ with Examples
- Difference between Increment and Decrement Operators
- Find two numbers with the given LCM and minimum possible difference
- Map of Vectors in C++ STL with Examples
- Problem in comparing Floating point numbers and how to compare them correctly?
- exit(0) vs exit(1) in C/C++ with Examples
- C++ program to print all Even and Odd numbers from 1 to N
- Reverse the content of a file and store it in another
- Minimum cells to be flipped to get a 2*2 submatrix with equal elements