cc.
cc.

Reputation: 5633

How to use BigInteger?

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?

Upvotes: 158

Views: 349706

Answers (10)

NIlesh Pingale
NIlesh Pingale

Reputation: 1

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

Upvotes: -1

MarkPowell
MarkPowell

Reputation: 16530

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));

Upvotes: 208

Arpna Joshi
Arpna Joshi

Reputation: 37

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

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

Upvotes: 0

murali kurapati
murali kurapati

Reputation: 1586

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

Upvotes: 4

frank.liu
frank.liu

Reputation: 475

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.

Upvotes: -7

harry
harry

Reputation: 41

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));

Upvotes: -2

Arvind
Arvind

Reputation: 1568

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));

Upvotes: 12

Dean J
Dean J

Reputation: 40288

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));
    }
}

Upvotes: 24

Poindexter
Poindexter

Reputation: 2425

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

Upvotes: 12

Bozho
Bozho

Reputation: 597016

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.

Upvotes: 58

Related Questions