How do I write BigInteger in Java?

Asked 2 years ago, Updated 2 years ago, 144 views

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

I wrote it like this. The value of sum is always 0. Is there something wrong?

integer java

2022-09-22 22:10

1 Answers

BigInteger is imutable, so the value does not change. Therefore, the sum value does not change. You should reallocate the value of sum in the add method.

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

Like this.


2022-09-22 22:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.