Start free

Intermediate · Functions · 110 of 264

Least common multiple

Write lcm(a, b), the smallest number both a and b divide into. Tip: lcm(a, b) is a * b // gcd(a, b).

Input: two positive whole numbers, one per line. Output: their least common multiple.

Examples

Input
4
6
Output
12
Input
7
3
Output
21
Input
12
12
Output
12

One more test checks other inputs when you press Check.

Stuck? The idea behind functions

A function, made with def, gives a name to a piece of code, so it can be used again: its parameters are the values it is given, return hands an answer back.

A function that returns lets the caller decide what to do with the answer: print it, compare it, use it in a sum.

def is_even(n):
    return n % 2 == 0

print(is_even(4))
print(is_even(7))

Watch for: print inside a function is not return: the caller gets None. And the code after return in the same block never runs.

All the functions exercises
Your code
Opening the code editor…
Python 3.14 in your browser
Output
Run shows what your program prints. Check tests it on every example and the hidden tests.

More functions exercises