找到最接近某个值的公约数的有效算法?
我有两个数字,x1
和 x2
。对于数字y
,我想计算尽可能接近y
的x1
和x2
的公约数。
有一个有效的算法吗?
我认为是时候重新表述我的问题并使其更加清晰。这与整数无关...... 因此,假设我们有两个数字 x1
和 x2
。比如说,用户输入一个数字y
。我想找到的是一个接近于 y
的数字 y'
,这样 x1 % y'
和 x2 % y'< /code> 非常小(例如小于
0.02
,但我们称这个数字为 LIMIT
)。换句话说,我不需要最优算法,而是一个好的近似值。
我感谢大家付出的时间和精力,真是太好了!
I have two numbers, x1
and x2
. For a number y
, I want to calculate the common divisor of x1
and x2
as close as possible to y
.
Is there an efficient algorithm for this?
I believe it's time to rephrase my problem and be more clear. This is not about integers...
So, say we have two numbers x1
and x2
. Say, the user inputs a number y
. What I want to find, is a number y'
close to y
so that x1 % y'
and x2 % y'
are very small (smaller than 0.02
, for example, but lets call this number LIMIT
). In other words, I don't need an optimal algorithm, but a good approximation.
I thank you all for your time and effort, that's really kind!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我相信这个问题没有已知的有效(多项式时间)算法,因为从 整数分解来解决这个问题。由于没有已知的用于整数分解的多项式时间算法,因此也不存在用于您的问题的已知算法,因为否则我们确实会有一个用于整数分解的多项式时间算法。
要了解其工作原理,假设您有一个要分解的数字 n。现在,使用您想要的任何算法,找到 n 和最接近 √n 的 n 的公因数。由于 n 的非平凡除数不能大于 √n,因此可以找到 (1) 可整除 n 的最大整数,或 (2) 如果 n 是质数,则找到数字 1。然后,您可以将 n 除以该数字并重复以产生 n 的所有因数。由于 n 最多可以有 O(log n) 个因子,因此这最多需要对问题的求解器进行多项式多次迭代,因此我们从整数因式分解到此问题的多项式时间减少。如上所述,这意味着,至少在公开文献中,还没有已知的有效经典算法来解决这个问题。一个可能存在,但这将是一个非常重要的结果。
很抱歉给出否定的答案,希望这对您有所帮助!
I believe that there is no known efficient (polynomial-time) algorithm for this problem because there is a polynomial-time reduction from integer factorization to this problem. Since there is no known polynomial-time algorithm for integer factorization, there cannot be a known algortihm for your problem either, since otherwise we would indeed have a polynomial-time algorithm for integer factorization.
To see how this works, suppose you have a number n that you'd like to factor. Now, using whatever algorithm you'd like, find the common factor of n and n closest to √n. Since no nontrivial divisor of n can be greater than √n, this finds either (1) the largest integer that divides n, or (2) the number 1 if n is prime. You can then divide n by this number and repeat to produce all the factors of n. Since n can have at most O(log n) factors, this requires at most polynomially many iterations of the solver for your problem, so we have a polynomial-time reduction from integer factorization to this problem. As mentioned above, this means that, at least in the open literature, there is no known efficient classical algorithm for solving this problem. One might exist, but it would be a really hugely important result.
Sorry for the negative answer, and hope this helps!
这是有效的,因为我可以得到它:
This is efficient as I can get it:
我认为你可以通过贪心算法来做到这一点,首先通过常见算法找到GCD,将其命名为
d
(可以用对数时间计算),然后每次除<时找到d
的因子< code>d 为最小可用因子(创建d'
),并将|d'-y|
与|dy|
进行比较如果更小,继续以这种方式(并替换d'
与d
),否则,将d'
与最小消除因子相乘,并再次将其距离与 y 进行比较。I think you can do it by greedy algorithm, first find GCD by common algorithms name it
d
(which is computable in logarithmic time) then find factors ofd
each time divided
to smallest available factor (created'
), and compare|d'-y|
with|d-y|
if is smaller continue in this way (and replaced'
withd
), else, multiplyd'
with smallest eliminated factor, and again compare its distance to y.x1
和x2
的 GCD。GCD <= Y
则返回GCD
GCD
,最佳距离为GCD - y
>。x1
和x2
倍数的第一个整数查找 GCD
查找最接近的除数到 y...
我相信唯一可用的额外优化是按照 @trinithis 的建议分解 gcd(也许使用筛子?)。
x1
andx2
.GCD <= Y
then returnGCD
GCD
, with a best distance ofGCD - y
.x1
andx2
To find the GCD
To find closest divisor to y...
I believe the only additional optimization available would be to factor the gcd (perhaps using a sieve?) as @trinithis suggested.