How do I write BigInteger in Java?

Asked 1 years ago, Updated 1 years ago, 128 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.