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.

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.
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 #1

Test #2

Test #3

Arguments

a = 4
b = 6

Expected Output

12