Menu
CoddyTech

Least Common Multiple

You get two positive integers a and b. Return their least common multiple: the smallest positive integer that both a and b divide with no remainder.

For example, the multiples of 6 are 6, 12, 18, 24 and so on, the multiples of 8 are 8, 16, 24 and so on, and the first number on both lists is 24.

Function

lcm(a: integer, b: integer) → integer
ainteger
the first positive integer
binteger
the second positive integer
Returnsinteger
the smallest positive integer that is a multiple of both a and b

Constraints

  • 1 ≤ a ≤ 106
  • 1 ≤ b ≤ 106
  • The answer fits in a signed 32-bit integer: lcm(a, b) ≤ 231-1. The product a × b may not.

Examples

Input
a = 4b = 6
Output
12
Explanation
The multiples of 6 start 6, 12, 18; the multiples of 4 start 4, 8, 12. The first number on both lists is 12.

lock icon+15 hidden tests on Submit

challenge icon

Follow-up

Can you find the gcd with no division or remainder at all, using only subtraction and halving?

Reset code
def lcm(a, b):
    # Write code here
Test cases

Case 1

Case 2

Case 3

Input

a = 4
b = 6

Expected

12