Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 如何从lcm(a,b)=c中找到最小的b?_Algorithm_Lcm - Fatal编程技术网

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