Легко Решено: 0 Процент решений: 0%

Least Common Multiple

Find the smallest positive number divisible by both values.

Чему вы научитесь

  • Compute the LCM from the GCD
  • Avoid overflow by dividing before multiplying
  • Reuse one function inside another

Условие

Показать перевод

Return the least common multiple of a and b, the smallest positive number that both divide. lcm(a, b) = a / gcd(a, b) × b.

Implement lcm(a, b).

Input

a: The first positive number. b: The second positive number.

Output

Return lcm(a, b).

Ограничения

1 ≤ a, b ≤ 1,000,000,000

Примеры

Пример 1
Ввод:
a = 4
b = 6

Ожидаемый вывод:
expected = 12
Ввод:
long long a = 4;
long long b = 6;

Ожидаемый вывод:
long long expected = 12;
Ввод:
long a = 4;
long b = 6;

Ожидаемый вывод:
long expected = 12;

Пояснение

Multiples of 4: 4, 8, 12…; multiples of 6: 6, 12…; the first common one is 12.

Пример 2
Ввод:
a = 7
b = 7

Ожидаемый вывод:
expected = 7
Ввод:
long long a = 7;
long long b = 7;

Ожидаемый вывод:
long long expected = 7;
Ввод:
long a = 7;
long b = 7;

Ожидаемый вывод:
long expected = 7;

Пояснение

A number's LCM with itself is the number: 7.

Как подступиться

Сначала попробуйте сами, затем открывайте подсказки по одной.

Подсказка 1

Use the relationship between gcd and lcm instead of searching for multiples.

Подсказка 2

Compute the gcd first, then combine the two numbers carefully.

Подсказка 3

Divide before multiplying to reduce overflow risk, even with large inputs.

Ожидаемая сложность

O(log min(a, b)) time, O(1) extra space

Ссылки

Это задание адаптировано для CodeUtility. Следуйте условию и ограничениям выше.

Учитесь у других

Решения сообщества

Пока нет опубликованных решений. Решите задание, чтобы поделиться первым.