oneby7

Foundations Working with digits and divisors

36. LCM of Two Numbers

Medium

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

Output: 36

Explanation: 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

Output: 25

Explanation: 5 already divides 25, so 25 is a multiple of both. No smaller multiple of 25 exists.

Example 3

Input: a = 1000000000000000, b = 999000

Output: 999000000000000000

Explanation: 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.

a = 6, b = 8

Constraints

  • 1 ≤ a, b ≤ 1015
  • The LCM of a and b is at most 1018.

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.