最接近一定的价值公约数逼近? [英] Approximation of a common divisor closest to some value?
问题描述
假设我们有两个数(不一定是整数) X1
和 X2
。这样说来,用户输入一个数是
。我想找到,是一个数 Y'
靠近是
让 X1% Y'
和 X2%Y
非常小(小于 0.02
,例如小,但让拨打此号码限制
)。换句话说,我并不需要一个最佳的算法,但是一个很好的近似。
Say we have two numbers (not necessarily integers) 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!
让我解释一下这个问题是在我的应用程序是什么的:说了,屏幕大小,给出的宽度屏幕宽度
和高度对 screenHeight
(像素)。我填充屏幕的长度 Y'
的平方。再说了,用户希望平方大小为是
。如果是
不是屏幕宽度
和/或 screenHeight
,将有在屏幕,没有足够大,以适应正方形的侧面的未使用的空间。如果未使用的空间很小(例如一行像素),它不是那么糟糕,但如果它不是,也不会好看。我怎样才能找到共同的除数屏幕宽度
和 screenHeight
?
Let me explain what the problem is in my application : say, a screen size is given, with a width of screenWidth
and a height of screenHeight
(in pixels). I fill the screen with squares of a length y'
. Say, the user wants the square size to be y
. If y
is not a divisor of screenWidth
and/or screenHeight
, there will be non-used space at the sides of the screen, not big enough to fit squares. If that non-used space is small (e.g. one row of pixels), it's not that bad, but if it's not, it won't look good. How can I find common divisors of screenWidth
and screenHeight
?
推荐答案
我看不出你如何能确保X1%Y'和x2%Y'均低于一定的价值 - 如果X1是素数,没有任何事情以低于你的限制(如限制为小于1),除了X1(或非常接近)和1。
I don't see how you can ensure that x1%y' and x2%y' are both below some value - if x1 is prime, nothing is going to be below your limit (if the limit is below 1) except x1 (or very close) and 1.
所以永远奏效的唯一答案是微不足道的Y'= 1。
So the only answer that always works is the trivial y'=1.
如果您允许非整数的除数,然后随便挑Y'= 1 /(X1 * X2),因为其余始终为0。
If you are permitting non-integer divisors, then just pick y'=1/(x1*x2), since the remainder is always 0.
在不限制公约数为整数,它可以是任何东西,而整个最大公约数的概念就走出了窗外。
Without restricting the common divisor to integers, it can be anything, and the whole 'greatest common divisor' concept goes out the window.
这篇关于最接近一定的价值公约数逼近?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!