如何使用大整数?

发布于 2024-08-11 17:58:17 字数 219 浏览 2 评论 0原文

我有这段代码,它不起作用:

BigInteger sum = BigInteger.valueOf(0);
for(int i = 2; i < 5000; i++) {
    if (isPrim(i)) {
        sum.add(BigInteger.valueOf(i));
    }
}

sum 变量始终为 0。我做错了什么?

I have this piece of code, which is not working:

BigInteger sum = BigInteger.valueOf(0);
for(int i = 2; i < 5000; i++) {
    if (isPrim(i)) {
        sum.add(BigInteger.valueOf(i));
    }
}

The sum variable is always 0. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(10

北方。的韩爷 2024-08-18 17:58:17

BigInteger 是不可变的。 javadocs 指出 add() “[r]返回一个 BigInteger,其值为 (this + val)。”因此,您无法更改 sum,您需要将 add 方法的结果重新分配给 sum 变量。

sum = sum.add(BigInteger.valueOf(i));

BigInteger is immutable. The javadocs states that add() "[r]eturns a BigInteger whose value is (this + val)." Therefore, you can't change sum, you need to reassign the result of the add method to sum variable.

sum = sum.add(BigInteger.valueOf(i));
壹場煙雨 2024-08-18 17:58:17
sum = sum.add(BigInteger.valueOf(i))

BigInteger 类是不可变的,因此您无法更改其状态。因此,调用“add”会创建一个新的 BigInteger,而不是修改当前的 BigInteger。

sum = sum.add(BigInteger.valueOf(i))

The BigInteger class is immutable, hence you can't change its state. So calling "add" creates a new BigInteger, rather than modifying the current.

无可置疑 2024-08-18 17:58:17

其他回复已经指出了这一点; BigInteger 是不可变的。这是使该代码正常工作的微小更改。

BigInteger sum = BigInteger.valueOf(0);
for(int i = 2; i < 5000; i++) {
    if (isPrim(i)) {
        sum = sum.add(BigInteger.valueOf(i));
    }
}

Other replies have nailed it; BigInteger is immutable. Here's the minor change to make that code work.

BigInteger sum = BigInteger.valueOf(0);
for(int i = 2; i < 5000; i++) {
    if (isPrim(i)) {
        sum = sum.add(BigInteger.valueOf(i));
    }
}
手心的温暖 2024-08-18 17:58:17

BigInteger 是一个不可变的类。因此,每当您进行任何算术运算时,都必须将输出重新分配给变量。

BigInteger is an immutable class. So whenever you do any arithmetic, you have to reassign the output to a variable.

决绝 2024-08-18 17:58:17

java.math.BigInteger 是一个不可变类,因此我们不能在已分配对象的位置分配新对象。但是您可以创建新对象来分配新值,例如:

sum = sum.add(BigInteger.valueOf(i));

java.math.BigInteger is an immutable class so we can not assign new object in the location of already assigned object. But you can create new object to assign new value like:

sum = sum.add(BigInteger.valueOf(i));
倒数 2024-08-18 17:58:17

是的,它是不可变的

sum.add(BigInteger.valueOf(i));

,因此 BigInteger 类的方法 add() 不会将新的 BigInteger 值添加到其自己的值中,但是创建并返回一个新的 BigInteger 引用而不更改当前的 BigInteger 并且这就是什么即使在字符串的情况下也可以完成

Yes it's Immutable

sum.add(BigInteger.valueOf(i));

so the method add() of BigInteger class does not add new BigIntger value to its own value ,but creates and returns a new BigInteger reference without changing the current BigInteger and this is what done even in the case of Strings

予囚 2024-08-18 17:58:17

Biginteger 是一个不可变的类。
您需要将输出的值显式分配给 sum,如下所示:

sum = sum.add(BigInteger.valueof(i));    

Biginteger is an immutable class.
You need to explicitly assign value of your output to sum like this:

sum = sum.add(BigInteger.valueof(i));    
甜味超标? 2024-08-18 17:58:17

假设有时我们的输入值太大而无法存储整数值,例如123456789123456789
在这种情况下,像 long 这样的标准数据类型就无法处理它。但是Java提供了一个类“BigInteger”。它帮助我们传递和获取巨大的价值。
请参阅下面的考试以明确概念。

import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        BigInteger B1=new BigInteger("10");
        BigInteger B2=new BigInteger("20");
        B1=sc.nextBigInteger();
        B2=sc.nextBigInteger();
        Solution obj=new Solution();
        obj.Big_Int(B1,B2);
        sc.close();
    }
    
    public void Big_Int(BigInteger a,BigInteger b)
    {
        //BigInteger bi=new BigInteger("1");
        BigInteger bi=BigInteger.TEN;
        bi=a;
        bi=bi.add(b);
        System.out.println(bi);
        bi=a;
        bi=bi.multiply(b);
        System.out.println(bi);
        
    }
}

输入:
123456789123456789
1578426354785

输出:
123458367549811574
194867449629598334799730885365

Suppose sometimes our input value is too big to store integer value eg.123456789123456789
In that case, standard datatype like long can't handle it. But Java provides a class "BigInteger". It helps us to pass and access huge value.
Please see the below exam to clear the concept.

import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        BigInteger B1=new BigInteger("10");
        BigInteger B2=new BigInteger("20");
        B1=sc.nextBigInteger();
        B2=sc.nextBigInteger();
        Solution obj=new Solution();
        obj.Big_Int(B1,B2);
        sc.close();
    }
    
    public void Big_Int(BigInteger a,BigInteger b)
    {
        //BigInteger bi=new BigInteger("1");
        BigInteger bi=BigInteger.TEN;
        bi=a;
        bi=bi.add(b);
        System.out.println(bi);
        bi=a;
        bi=bi.multiply(b);
        System.out.println(bi);
        
    }
}

input:
123456789123456789
1578426354785

output:
123458367549811574
194867449629598334799730885365

水水月牙 2024-08-18 17:58:17

实际上你可以使用,

BigInteger sum= new BigInteger("12345");

为 BigInteger 类创建对象。但是这里的问题是,你不能在双引号中给出变量。所以我们必须使用 valueOf() 方法,并且我们必须存储再次得出该总和的答案。所以我们会写,

sum= sum.add(BigInteger.valueOf(i));

Actually you can use,

BigInteger sum= new BigInteger("12345");

for creating object for BigInteger class.But the problem here is,you cannot give a variable in the double quotes.So we have to use the valueOf() method and we have to store the answer in that sum again.So we will write,

sum= sum.add(BigInteger.valueOf(i));
开始看清了 2024-08-18 17:58:17

由于您正在将一些 int 值相加,因此无需使用 BigInteger。 long 就足够了。 int 是 32 位,而 long 是 64 位,可以包含所有 int 值的总和。

Since you are summing up some int values together, there is no need to use BigInteger. long is enough for that. int is 32 bits, while long is 64 bits, that can contain the sum of all int values.

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