Algorithm 如何从lcm(a,b)=c中找到最小的b?
我知道Algorithm 如何从lcm(a,b)=c中找到最小的b?,algorithm,lcm,Algorithm,Lcm,我知道a和c。如果lcm(a,b)=c,我如何找到最小的b?您可以使用。在这里,lcm(a,b)必须包含a和b的所有基本因子,它们出现在两个数字中的任意一个的最高倍数中 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,
a
和c
。如果lcm(a,b)=c
,我如何找到最小的b
?您可以使用。在这里,lcm(a,b)
必须包含a
和b
的所有基本因子,它们出现在两个数字中的任意一个的最高倍数中
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.
例如,8=2^3
和12=2^2*3
,因此lcm(8,12)=2^3*3=24
这很容易逆转:找到c
的主要因素(包括它们的多重性),然后检查a
已经涵盖了哪些因素<代码>b必须是其余代码的乘积
因此,如果
c=24=2^3*3
和a=6=2*3
,那么b
必须是8=2^3
。a
已经涵盖了3^1
,但是a
只有2^1
,所以b
必须是2^3
如果c
是lcm(a,b)
,c%a=0
那么
如果存在b,最小的b是c。@rsy56640对不起,不是gcd…最不常见的多重b应该是
c/a
,所以我必须从gcd(a/d,n)=1中找到d?如何计算?您应该找到a的因子,然后遍历因子数组以找到最小的d,这样gcd(a/d,n)=1这似乎不正确。使用lcm(6,b)=24时会发生什么gcd(6,24/6)=2
,那么b=24
?但是b=8
也是有效的,而且更小。O,这是真的。错过了这个案子。
if gcd(a, c/a) == 1:
b = c/a
else
b = c