【发布时间】:2013-06-04 20:06:50
【问题描述】:
我正在使用二进制方法来计算两个分数的 GCD,该方法工作得非常好,除非我将某些数字相减。
我假设这是因为,例如,当我从 1/6 中减去 2/15 时,GCD 有一个重复的数字或类似的东西,尽管我可能是错的。
//The following lines calculate the GCD using the binary method
if (holderNum == 0)
{
gcd = holderDem;
}
else if (holderDem == 0)
{
gcd = holderNum;
}
else if ( holderNum == holderDem)
{
gcd = holderNum;
}
// Make "a" and "b" odd, keeping track of common power of 2.
final int aTwos = Integer.numberOfTrailingZeros(holderNum);
holderNum >>= aTwos;
final int bTwos = Integer.numberOfTrailingZeros(holderDem);
holderDem >>= bTwos;
final int shift = Math.min(aTwos, bTwos);
// "a" and "b" are positive.
// If a > b then "gdc(a, b)" is equal to "gcd(a - b, b)".
// If a < b then "gcd(a, b)" is equal to "gcd(b - a, a)".
// Hence, in the successive iterations:
// "a" becomes the absolute difference of the current values,
// "b" becomes the minimum of the current values.
if (holderNum != gcd)
{
while (holderNum != holderDem)
{
//debuging
String debugv3 = "Beginning GCD binary method";
System.out.println(debugv3);
//debugging
final int delta = holderNum - holderDem;
holderNum = Math.min(holderNum, holderDem);
holderDem = Math.abs(delta);
// Remove any power of 2 in "a" ("b" is guaranteed to be odd).
holderNum >>= Integer.numberOfTrailingZeros(holderNum);
gcd = holderDem;
}
}
// Recover the common power of 2.
gcd <<= shift;
这是我用来完成此操作的代码,调试消息将永远打印出来。
有没有办法在卡住时作弊,或者设置一个例外?
【问题讨论】:
-
你有没有试过打印一些有用的东西,比如“holderNum”和“holderDem”的值?这样你就可以看到数字在做什么,而不是猜测它。
-
如何保证“b”为奇数?
holderNum和holderDem的值是多少,是否进入了无限循环? -
是的,当我打印出来时,使用给出的示例,holderNum 保持在-3,而 holderDem 不断增加,1195095 是我强制退出之前的位置。
-
对于什么值?它对我有用。
-
给出的例子,例如2/15减去1/6。或 1/1 减去 1/1。但是,如果我在那里休息一下,它就可以正常工作,我想这可以吗?
标签: java infinite-loop greatest-common-divisor