EasyFoundational
Find the Least Common Multiple
- Time limit
- 1 s
- Memory limit
- 128 MB
- Estimated
- 15 min
- Programming Fundamentals
- GCD and Divisibility
Description
Given two positive integers a and b, return their least common multiple (LCM).
The least common multiple is the smallest positive integer that is divisible by both numbers. For example, the LCM of 4 and 6 is 12, because 12 is the smallest positive number divisible by both.
You may use the relationship LCM(a, b) = (a / GCD(a, b)) * b. The division should be performed before multiplication to reduce the risk of intermediate overflow.
Both inputs are positive, so the returned LCM is always positive.
Implement lcm(a, b).
Constraints
1 <= a, b <= 1000000 The LCM of a and b is guaranteed to fit within a signed 64-bit integer.
Sample cases
3Sample 1
Input
a = 4 b = 6
Expected output
12
Sample 2
Input
a = 5 b = 7
Expected output
35
Sample 3
Skills and languages
Concepts
- Euclidean Algorithm
- Integer Math Utilities
Languages
- Python 3
Solve this problem
Create a free account to write, run and submit your solution.
- Write Python or Java in the browser, with your draft saved as you type
- Run your code on the examples as often as you like
- Submit to a sandboxed judge and see exactly which case failed
- Ask the AI mentor for a hint without being handed the answer