20

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.

Progo
  • 3,452
  • 5
  • 27
  • 44
  • Well, why do you think that isn't working? – E_net4 Sep 13 '14 at 15:38
  • @E_net4 Um... I know why it isn't working. I am looking for a solution. – Progo Sep 13 '14 at 15:50
  • 3
    That is a LOT of code if what you're asking for is "compare BigInt with an int". Is there another question hidden in there? Otherwise: http://docs.oracle.com/javase/6/docs/api/java/math/BigInteger.html#compareTo(java.math.BigInteger) `compareTo` returns -1 (less than), 0 (equal), or 1 (greater than) – Gus Sep 13 '14 at 15:51

3 Answers3

36

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
}
Joe
  • 29,416
  • 12
  • 68
  • 88
  • 3
    +1 for casting the int into a BigInt, vs the other way around. Might want to mention why – Gus Sep 13 '14 at 15:55
5

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.
 } 
Community
  • 1
  • 1
Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
  • 1
    Although it is an issue in the question's code snippet, that doesn't really answer the main question. – E_net4 Sep 13 '14 at 15:39
3

Just use BigInteger.compare:

int myInt = ...;
BigInteger myBigInt = ...;
BigInteger myIntAsABigInt = new BigInteger(String.valueOf(myInt));

if (myBigInt.compareTo(myIntAsABigInt) < 0) {
    System.out.println ("myInt is bigger than myBigInt");
} else if (myBigInt.compareTo(myIntAsABigInt) > 0) {
    System.out.println ("myBigInt is bigger than myInt");
} else {
    System.out.println ("myBigInt is equal to myInt");
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350