1. Problem Definition
The Least Common Multiple (LCM) refers to the smallest common multiple among the multiples of two or more integers.
For example, the least common multiple of 4 and 5 is 20. This is because 20 is the smallest number shared among the multiples of 4 (4, 8, 12, 16, 20, …) and the multiples of 5 (5, 10, 15, 20, …). In this course, we will solve the problem of finding the least common multiple of two given numbers using JavaScript.
2. Problem Description
Write a function that takes two integers as input and returns their least common multiple.
Function Signature: function lcm(a: number, b: number): number
Input:
- Two integers a, b (1 ≤ a, b ≤ 106)
Output:
- The least common multiple of a and b
Examples:
- Input: 4, 5 => Output: 20
- Input: 15, 20 => Output: 60
- Input: 7, 5 => Output: 35
3. Algorithm Approach
There are several ways to find the least common multiple. However, one of the most common methods is using the Greatest Common Divisor (GCD).
The least common multiple can be calculated using the following formula:
LCM(a, b) = (a * b) / GCD(a, b)
One efficient algorithm to find GCD is the Euclidean algorithm.
The Euclidean algorithm calculates the GCD of two integers as follows:
- Let r be the remainder when a is divided by b; then, GCD(a, b) = GCD(b, r) holds true.
- When r is 0, b is the GCD.
Now, let’s implement a function in JavaScript to find LCM based on this logic.
4. Code Implementation
function gcd(a, b) {
while (b !== 0) {
let temp = b;
b = a % b;
a = temp;
}
return a;
}
function lcm(a, b) {
return (a * b) / gcd(a, b);
}
// Test cases
console.log(lcm(4, 5)); // 20
console.log(lcm(15, 20)); // 60
console.log(lcm(7, 5)); // 35
The above code is implemented to calculate GCD and use it to find LCM. The gcd function returns the GCD of a and b. The lcm function calculates and returns the LCM of the two numbers.
5. Code Explanation
gcd function:
- This function takes two integers as arguments and calculates the GCD.
- It uses a while loop to repeat until b is 0.
- In each iteration, it finds the remainder of a divided by b, assigns b to a, and the remainder to b.
- When b becomes 0, the value of a contains the GCD, which is returned.
lcm function:
- This function takes two integers as arguments and calculates the LCM.
- It calls GCD(a, b) to find the GCD and then calculates and returns LCM(a, b).
6. Optimization & Considerations
The above algorithm is efficient. GCD has a time complexity of O(log(min(a, b))), so the time needed for LCM calculation is also minimized.
However, the following considerations should also be taken into account:
- Handling negative numbers: While the problem restricts the range of integers to 1 and above, it may be beneficial to add exception handling to account for negatives in actual use.
- Handling maximum values: The product of a and b can become very large, potentially leading to overflow during calculation. In such cases, it is necessary to consider methods that can handle large numbers, like BigInt.
7. Conclusion
In this course, we studied the algorithm to find the least common multiple of two integers. We learned how to efficiently calculate GCD using the Euclidean algorithm and then use it to calculate LCM.
Such algorithms can be practically useful in various problems. In the next course, we will cover another algorithmic topic. Thank you.