关于 Java BigInteger

发布于 2024-12-28 00:22:24 字数 972 浏览 3 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

献世佛 2025-01-04 00:22:24

BigInteger 类将整数表示为不可变对象。

这里有两点。

  1. 不要使用 == 来测试两个 BigInteger 是否相等
  2. 要更改 BigInteger 变量的值,您必须执行 i = i.add(ONE) ; 而不仅仅是 i.add(ONE);

不使用 == 比较 BigInteger 的原因是,您不是检查数值相等,而是检查它们是否是内存中的同一对象。

考虑使用String

String a = "a";
String b = "b";

String x = a + b;
String y = "ab";

在上面的示例中,x.equals(y) 为 true,因为它们包含相同数量且顺序完全相同的字符。然而,x == y 并不成立,因为它们是内存中的不同对象。

需要将算术运算的结果分配给变量的原因是 BigInteger 是不可变的。因此算术运算不能改变它正在操作的对象的值,但它可以创建一个新的 BigInteger (它返回)。这就是为什么您必须将算术运算的结果分配给您想要保存它的变量。

顺便说一句,您的 lesserThanOrEqual 的快捷方式是这样的。

boolean result = m.compareTo(n) <= 0;

基本上

  • 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.

  1. Don't use == to test if two BigIntegers are equal
  2. To change the value of a BigInteger variable you must do i = i.add(ONE); and not just i.add(ONE);.

The reason not to use == to compare BigIntegers is because instead of checking for numerical equality you are checking that they are the same object in memory.

Consider with Strings.

String a = "a";
String b = "b";

String x = a + b;
String y = "ab";

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.

boolean result = m.compareTo(n) <= 0;

Basically

  • m == n becomes m.compareTo(n) == 0
  • m != n becomes m.compareTo(n) != 0
  • m < n becomes m.compareTo(n) < 0
  • m > n becomes m.compareTo(n) > 0
  • m <= n becomes m.compareTo(n) <= 0
  • m >= n becomes m.compareTo(n) >= 0
梦毁影碎の 2025-01-04 00:22:24

以上两个答案都是正确的。然而,他们并没有告诉您 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...

丑疤怪 2025-01-04 00:22:24

你正在这样做:

i.add(ONE);

但你应该这样做:

i = i.add(ONE);

You are doing this:

i.add(ONE);

But you should do this:

i = i.add(ONE);
花开柳相依 2025-01-04 00:22:24

您没有保存从 i.add(ONE) 返回的结果。它为您提供了一个包含所需值的 BigInteger 对象,但您将其扔在地板上,而不是将其分配给 i

You're not saving the result that gets returned from i.add(ONE). It's giving you a BigInteger object containing the desired value, but you're dropping it on the floor instead of assigning it to i.

优雅的叶子 2025-01-04 00:22:24

i.add(ONE) 必须重新分配:i = i.add(ONE)

i.add(ONE) has to be reassigned: i = i.add(ONE)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文