关于 Java BigInteger
我是 Java 新手,刚刚使用 BigInteger 编写了一个程序。
public static void main(String[] args) {
BigInteger n = new BigInteger("5");
BigInteger i = new BigInteger("2");
while (lesserOrEqual(i,n) {
System.out.println("n.mod(i) = "+n.mod(i));
if (n.mod(i) == ZERO) {
n = n.divide(i);
}
else {
i.add(ONE);
}
System.out.println("n = "+n);
System.out.println("i = "+i);
}
public static boolean lesserOrEqual(BigInteger m, BigInteger n) `{
if (m.compareTo(n) == -1 || m.compareTo(n) == 0)
return true;
return false;
}
ZERO 和 ONE 分别定义为 BigInteger 0、1 类型。
我希望“i=2”除“n=5”,如果“n mod i == 0”,否则“i++”,直到“n”小于或等于“i”。
我认为输出必须是
n.mod(i) = 1
n = 5
i = 3
n.mod(i) = 2
n = 5
i = 4
n.mod(i) = 1
n = 5
i = 5
n.mod(i) = 0
n = 1
i = 5
,并且使用原始类型 int 的等效代码,我得到了预期的结果。
但这与 BigInteger 会进入无限循环
n.mod(i) = 1
n = 5
i = 2
...
有谁知道为什么会这样?
I'm new to Java and just code a program with BigInteger.
public static void main(String[] args) {
BigInteger n = new BigInteger("5");
BigInteger i = new BigInteger("2");
while (lesserOrEqual(i,n) {
System.out.println("n.mod(i) = "+n.mod(i));
if (n.mod(i) == ZERO) {
n = n.divide(i);
}
else {
i.add(ONE);
}
System.out.println("n = "+n);
System.out.println("i = "+i);
}
public static boolean lesserOrEqual(BigInteger m, BigInteger n) `{
if (m.compareTo(n) == -1 || m.compareTo(n) == 0)
return true;
return false;
}
ZERO and ONE are defined of the type BigInteger 0, 1, respectively.
I want "i=2" to divide "n=5", if "n mod i == 0", else "i++", until "n" to be lesser or equal to "i".
I think the output must be
n.mod(i) = 1
n = 5
i = 3
n.mod(i) = 2
n = 5
i = 4
n.mod(i) = 1
n = 5
i = 5
n.mod(i) = 0
n = 1
i = 5
and with the equivalent code with primitive type int, I have the result as expected.
but this with BigInteger goes to infinite loop
n.mod(i) = 1
n = 5
i = 2
...
Does anyone know why it is so?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
BigInteger 类将整数表示为不可变对象。
这里有两点。
==
来测试两个BigInteger
是否相等i = i.add(ONE) ;
而不仅仅是i.add(ONE);
。不使用
==
比较 BigInteger 的原因是,您不是检查数值相等,而是检查它们是否是内存中的同一对象。考虑使用
String
。在上面的示例中,
x.equals(y)
为 true,因为它们包含相同数量且顺序完全相同的字符。然而,x == y
并不成立,因为它们是内存中的不同对象。需要将算术运算的结果分配给变量的原因是 BigInteger 是不可变的。因此算术运算不能改变它正在操作的对象的值,但它可以创建一个新的 BigInteger (它返回)。这就是为什么您必须将算术运算的结果分配给您想要保存它的变量。
顺便说一句,您的 lesserThanOrEqual 的快捷方式是这样的。
基本上
m == n
变成m.compareTo(n) == 0
m != n
变成m.compareTo(n) != 0
m < n
变为m.compareTo(n)
0
米> n
变为m.compareTo(n) > 0
m <= n
变为m.compareTo(n) <= 0
m >= n
变为m.compareTo(n) >= 0
The BigInteger class represents integers as immutable objects.
There are two points here.
==
to test if twoBigInteger
s are equali = i.add(ONE);
and not justi.add(ONE);
.The reason not to use
==
to compareBigInteger
s is because instead of checking for numerical equality you are checking that they are the same object in memory.Consider with
String
s.In the above example
x.equals(y)
is true because they contain the same number of characters in exactly the same order. However,x == y
is not true because they are different objects in memory.The reason you need to to assign the result of arithmetic operations to a variable is because BigInteger is immutable. Thus arithmetic operations cannot change the value of the object it is operating on, but it can create a new BigInteger (which it returns). Which is why you must assign the result of the arithmetic operation to the variable you want it saved in.
As an aside a shortcut for your lesserThanOrEqual to is this.
Basically
m == n
becomesm.compareTo(n) == 0
m != n
becomesm.compareTo(n) != 0
m < n
becomesm.compareTo(n) < 0
m > n
becomesm.compareTo(n) > 0
m <= n
becomesm.compareTo(n) <= 0
m >= n
becomesm.compareTo(n) >= 0
以上两个答案都是正确的。然而,他们并没有告诉您 BigInteger 实例是不可变的。这意味着它们一旦设置就不会改变。这就是为什么你需要总是分配转换的结果......
both of the above answers are right. They are not telling you, however, that BigInteger instances are immutable. That means they don't change once set. That is why you need to always assign the result of a transformation...
你正在这样做:
但你应该这样做:
You are doing this:
But you should do this:
您没有保存从
i.add(ONE)
返回的结果。它为您提供了一个包含所需值的BigInteger
对象,但您将其扔在地板上,而不是将其分配给i
。You're not saving the result that gets returned from
i.add(ONE)
. It's giving you aBigInteger
object containing the desired value, but you're dropping it on the floor instead of assigning it toi
.i.add(ONE)
必须重新分配:i = i.add(ONE)
i.add(ONE)
has to be reassigned:i = i.add(ONE)