Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java compare integer and bigInteger

How do I compare an int with a BigInteger in Java? I specifically need the know if an int is less than a BigInteger. Here is the code I am using:

private static BigInteger two = new BigInteger("2");
private static BigInteger three = new BigInteger("3");
private static BigInteger zero = new BigInteger("0");    
public static BigInteger bigIntSqRootCeil(BigInteger x) throws IllegalArgumentException {
    if (x.compareTo(BigInteger.ZERO) < 0) {
        throw new IllegalArgumentException("Negative argument.");
    }
    if (x == BigInteger.ZERO || x == BigInteger.ONE) {
        return x;
    }
    BigInteger two = BigInteger.valueOf(2L);
    BigInteger y;
    for (y = x.divide(two);
            y.compareTo(x.divide(y)) > 0;
            y = ((x.divide(y)).add(y)).divide(two));
    if (x.compareTo(y.multiply(y)) == 0) {
        return y;
    } else {
        return y.add(BigInteger.ONE);
    }
}
private static boolean isPrimeBig(BigInteger n){
    if (n.mod(two) == zero)
        return (n.equals(two));
    if (n.mod(three) == zero)
        return (n.equals(three));
    BigInteger m = bigIntSqRootCeil(n);
    for (int i = 5; i <= m; i += 6) {
        if (n.mod(BigInteger.valueOf(i)) == zero)
            return false;
        if(n.mod(BigInteger.valueOf(i + 2)) == zero)
            return false;
    };
    return true;
};

Thanks.

like image 467
Progo Avatar asked Sep 13 '14 15:09

Progo


People also ask

How does BigInteger compare to integer?

math. BigInteger. equals(Object x) method compares this BigInteger with the object passed as the parameter and returns true in both are equal in value else it returns false. Parameter: This method accepts a single mandatory parameter x which is the Object to which BigInteger Object is to be compared.

What is the difference between BigInteger and integer in Java?

BigInteger represents immutable arbitrary-precision integers. It is similar to the primitive integer types but allows arbitrary large values. It is used when integers involved are larger than the limit of long type. For example, the factorial of 50 is 30414093201713378043612608166064768844377641568960512000000000000.

How do I know if BigInteger is 1?

out. println(new BigInteger("1")==BigInteger. ONE); Ideally it should print true but its output is false.


2 Answers

How do I compare an int with a BigInteger in Java? I specifically need the know if an int is less than a BigInteger.

Turn the int into a BigInteger before comparing:

if (BigInteger.valueOf(intValue).compareTo(bigIntegerValue) < 0) {
  // intValue is less than bigIntegerValue
}
like image 56
Joe Avatar answered Oct 07 '22 21:10

Joe


Instead of

if (x == BigInteger.ZERO || x == BigInteger.ONE) {
    return x;

You should use :-

if (x.equals(BigInteger.ZERO) || x.equals(BigInteger.ONE)){
return x; 

Also, you should change the Integer first to BigInteger, and then compare, as mentioned by Joe in his answer:

 Integer a=3;
 if(BigInteger.valueOf(a).compareTo(BigInteger.TEN)<0){
    // your code...
 }
 else{
    // your rest code, and so on.
 } 
like image 20
Am_I_Helpful Avatar answered Oct 07 '22 21:10

Am_I_Helpful