π₯ GCD (Greatest Common Divisor) : [object Object]
- The GCD of
num1
andnum2
is the GCD ofnum2
andnum1 % num2
.
const getGCD = (num1, num2) => (num2 > 0 ? getGCD(num2, num1 % num2) : num1);
const getGCD = (num1, num2) => {
while (num2 > 0) {
let r = num1 % num2;
num1 = num2;
num2 = r;
}
return num1;
};
π₯ LCM (Least Common Multiple)
- First divide by gcd to prevent the overflow of the product of two numbers.
const getLCM = (num1, num2) => (num1 / getGCD(num1, num2)) * num2;