I know a
and c
. How can I find the least b
if lcm(a,b) = c
?
-
If b exsits, the least b is c. – rsy56640 Jun 02 '18 at 16:32
-
@rsy56640 sorry, not gcd...least common multiple – Александр Курмазов Jun 02 '18 at 16:33
-
Least b should be `c/a` – vish4071 Jun 02 '18 at 18:54
3 Answers
a = m*d, b = n*d, d = gcd(a,b), so c = lcm(a,b) = mnd;
Thus n = c/a. Notice that the less d, the less b.
so we can traverse the factor d of a, such that gcd(a/d, n) = 1.
the least d is what we need, then b = n*d.

- 299
- 1
- 3
- 13
-
-
you should find the factors of a, then traverse factor array to find the minimal d such that gcd(a/d,n)=1 – rsy56640 Jun 02 '18 at 16:51
You can find the least common multiple using prime factorization. Here, lcm(a, b)
has to contain all the prime factors of a
and b
in their highest multiple they appear in in any of the two numbers.
For example, 8 = 2^3
and 12 = 2^2 * 3
, so lcm(8, 12) = 2^3 * 3 = 24
.
This can easily be reversed: Find the prime factors of c
(including their multiplicity), then check which ones already are covered by a
. b
has to be the product of the remaining ones.
So if c = 24 = 2^3 * 3
and a = 6 = 2 * 3
, then b
has to be 8 = 2^3
. The 3^1
is already covered by a
, but a
only has 2^1
, so b
has to be 2^3
.

- 81,265
- 12
- 120
- 179
If c
is lcm(a, b)
, c%a=0
Then,
(gcd(a, c/a) * c) / a
gives the answer.
This follows from the fact that lcm(a,b) * gcd(a,b) = a * b
Also, gcd(a,b)
is easily implemented through euclid's algo, which solves your problem in logarithmic-complexity.

- 5,135
- 4
- 35
- 65
-
This does not seem right. What with `lcm(6,b)=24`? `gcd(6,24/6)=2`, so `b=24`? But `b=8` is also valid, and smaller. – tobias_k Jun 02 '18 at 20:19
-