Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using BigIntegers as a condition in while loop in Java

Tags:

java

I want to use a big integer value in my while loop condition but this is not working.

import java.util.Scanner;
import java.util.StringTokenizer;
public class NiceProbelm2 {
public static void main(String[]args){
    Scanner input = new Scanner(System.in);
    String number = input.nextLine();
    StringTokenizer st = new StringTokenizer(number);
    BigInteger base = null;
    int power=0;
    while (st.hasMoreTokens()) {
          String token1 = st.nextToken();
          String token2 = st.nextToken();
          base = new BigInteger(token1);
          power = Integer.parseInt(token2);


        }

        BigInteger result = base.pow(power);
        //long fresult = (long)result;
        BigInteger div = new BigInteger("10");
        System.out.println(result);
        BigInteger sum=null;
     //NOt working while(result.compareTo(new BigInteger("0")) > 10 )
                    {
        BigInteger digit = result.mod(div);
        result = result.divide(div);
        sum = sum.add(digit);

    }
     System.out.println(sum);
}

}

like image 529
Sheriffo Ceesay Avatar asked Dec 08 '25 06:12

Sheriffo Ceesay


2 Answers

You should never compare the return value of compareTo() to anything other than 0. (BigInteger.compareTo() is a bit more specific, but the same rule still applies)

You can check if it's greater than 0, less than 0 or equal to 0. Only those 3 pieces of information are actually relevant. The actual value (if it returns 1 or 10 or 100) doesn't matter.

like image 99
Joachim Sauer Avatar answered Dec 09 '25 19:12

Joachim Sauer


I think you problem with the previous solutions were you had a null pointer that was being thrown because your accumulator was not initialized. The following code produces the result you are looking for:

        BigInteger result = base.pow(power);
        BigInteger div = new BigInteger("10");
        System.out.println(result);
//The following line is different, initialize the sum before using it.
        BigInteger sum = BigInteger.ZERO;
        while(!BigInteger.ZERO.equals(result)) {
            BigInteger digit = result.mod(div);
            result = result.divide(div);
            sum = sum.add(digit);
        }
        System.out.println(sum);

I also wanted to point out that BigInteger has a method that provides both the quotient and remainder from a single division, which is more efficient than doing them both separately, and there is a "valueOf" method for BigInteger so you can use a numeric literal instead of a string:

    BigInteger result = base.pow(power);
    BigInteger div = BigInteger.valueOf(10);
    System.out.println(result);

    //Initialize the sum to zero
    BigInteger sum = BigInteger.ZERO;        
    //While the result has a non-zero decimal digit
    while(!BigInteger.ZERO.equals(result)) {
        //this divides by ten (first element),
        //and calculates the remainder (second element)
        BigInteger[] lastDigit = result.divideAndRemainder(div);
        result = lastDigit[0];
        sum = sum.add(lastDigit[1]);

    }
    System.out.println(sum);
like image 23
M. Jessup Avatar answered Dec 09 '25 19:12

M. Jessup