486.Find the LCM of Two Numbers
Easy
Given two positive integers
a and b, return their least common multiple (LCM) — the smallest positive number divisible by both.
The brute-forceBrute ForceChecking multiples of the larger number one at a time until one is also divisible by the smaller number. approach searches directly for the answer. The GCD formulaLCM via GCDThe identity LCM(a, b) × GCD(a, b) = a × b — rearranged to LCM(a, b) = (a / GCD(a, b)) × b, so the fast Euclidean GCD does all the real work. approach skips the search entirely: since a × b always equals LCM × GCD, finding the GCD first (fast, via Find the GCD of Two Numbers) hands over the LCM with one division and one multiplication.
Example 1:
Input: a = 4, b = 6
Output: 12
Example 2:
Input: a = 7, b = 13
Output: 91
Example 3:
Input: a = 5, b = 5
Output: 5
+ 4 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ a ≤ 10000 - ●
1 ≤ b ≤ 10000
a =
4
b =
6