Foundations Working with digits and divisors
36. LCM of Two Numbers
You are given two positive integers a and b. Return their least common multiple (LCM): the smallest
positive integer that both a and b divide with no remainder.
For example, the multiples of 4 are 4, 8, 12, 16 and so on, and the multiples of 6 are 6, 12, 18 and so on. The first number on both lists is 12, so the LCM of 4 and 6 is 12.
The answer can be larger than 253, so the JavaScript starter returns a BigInt.
Example 1
Input: a = 12, b = 18
36Explanation: The multiples of 18 are 18, 36, 54 and so on. 12 does not divide 18, but it divides 36, so the answer is 36.
Example 2
Input: a = 25, b = 5
25Explanation: 5 already divides 25, so 25 is a multiple of both. No smaller multiple of 25 exists.
Example 3
Input: a = 1000000000000000, b = 999000
999000000000000000Explanation: 999000 is 999 × 1000, and 1015 is already a multiple of 1000. So the smallest multiple of both is 1015 × 999.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ a, b ≤ 1015- The LCM of
aandbis at most 1018.
Clarifying questions
What you would ask an interviewer before writing any code.