In this guide, we will discuss the built-in functions in C++ that are used to compute the Least Common Multiple (LCM), along with their syntax and techniques.
When coding, it is common to need to find the Least Common Multiple (LCM) of two numbers. Instead of developing our own algorithm, we can simply use boost::math::lcm, a built-in function in the C++ boost library. To access this function, we need to include the appropriate header file.
<boost/math/common_factor.hpp>
Syntax:
It has the following syntax:
boost::math::lcm (m,n)
Parameters: m, n
Return Value: 0 if either value of m or n is zero; otherwise, the least common multiple of the absolute values of m and n.
Program:
Let's consider an example to calculate the least common multiple using the std::lcm function in C++.
#include <iostream>
#include <numeric>
using namespace std;
int main()
{
cout << "LCM(15, 25) = " << std::lcm(15, 25) << endl;
return 0;
}
Output:
LCM(15, 25) = 75
Essential points:
The function calculates the LCM by first finding the modulus of both integers. In case either of the numbers is negative, they are converted to their absolute value before computing the LCM.
The boost::math::lcm function is designed to raise an exception in case any of the numbers provided are not of integer data types.
Program:
Let's consider an illustration to determine the least common multiple using the boost::math::lcm function in C++.
// 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;
}
Due to the presence of a double type argument in the function, this code is expected to terminate prematurely and raise an exception.
Using std::lcm:
A recent addition to C++17 is the std::lcm function in the STL, which is compatible with compilers that have adopted C++17 standards. This function facilitates the calculation of the least common multiple (LCM) of two given values.
Syntax:
It has the following syntax:
std::lcm (m, n)
Arguments: m, n
Returns: 0 if either of m or n is 0
else, returns lcm of mod(m) and mod(n)
Program:
Let's consider an example to calculate the least common multiple using the std::lcm function in C++.
#include <iostream>
#include <numeric>
using namespace std;
int main()
{
cout << "LCM(18, 24) = " << std::lcm(18, 24) << endl;
return 0;
}
Output:
LCM(18, 24) = 72
Key Points:
- This function only accepts positive numbers as inputs; negative inputs are first transformed to their modulus before the LCM is computed.
- Additionally, it can only handle integer data types; an error will be raised if another data type, such as char or double, is supplied as an input.
There are some common functions for calculating LCM in C++:
Method 1: Calculating the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of two numbers using a for-loop.
In this illustration, the variable I is incremented from 0 up to the lesser value employing a for loop. The greatest common divisor (GCD) of two integers is determined when both numbers are divisible by i. If not, the GCD is adjusted accordingly. Subsequently, the least common multiple (LCM) of the two numbers is calculated based on the GCD of the two numbers.
Program:
#include <iostream>
using namespace std;
int main() {
int num1 = 30;
int num2 = 45;
int temp, gcd, lcm;
if (num1 > num2) {
temp = num1;
num1 = num2;
num2 = temp;
}
for(int i = 1; i <= num1; i++) {
if (num1 % i == 0 && num2 % i == 0)
gcd = i;
}
lcm = (num1 * num2) / gcd;
cout << "LCM of " << num1 << " and " << num2 << " is: " << lcm;
return 0;
}
Output:
LCM of 30 and 45 is: 90
Method 2: Determining the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of a pair of numbers using a while loop.
In the given illustration, the higher numerical value is replaced with a result derived by subtracting the lower number from the higher one. This process is iterated until both numbers are equal, leading to the calculation of the Greatest Common Divisor (GCD) of the two numbers. Subsequently, the Least Common Multiple (LCM) of the two numbers is determined based on the GCD value.
Program:
#include <iostream>
using namespace std;
int main() {
int num1 = 30;
int num2 = 45;
int x = num1;
int y = num2;
int lcm;
while (x != y) {
if (x > y)
x = x - y;
else
y = y - x;
}
lcm = (num1 * num2) / x;
cout << "LCM of " << num1 << " and " << num2 << " is: " << lcm;
return 0;
}
Output:
LCM of 30 and 45 is: 90