Java比较整数和大整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25824764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:14:33  来源:igfitidea点击:

Java compare integer and bigInteger

javabigintegerbigint

提问by Progo

How do I compare an intwith a BigIntegerin Java? I specifically need the know if an intis less than a BigInteger. Here is the code I am using:

我如何将 anintBigIntegerJava 中的a进行比较?我特别需要知道 anint是否小于 a BigInteger。这是我正在使用的代码:

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.

谢谢。

采纳答案by Joe

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

如何在 Java 中比较 int 和 BigInteger?我特别需要知道 int 是否小于 BigInteger。

Turn the intinto a BigIntegerbefore comparing:

int成一个BigInteger比较之前:

if (BigInteger.valueOf(intValue).compareTo(bigIntegerValue) < 0) {
  // intValue is less than bigIntegerValue
}

回答by Am_I_Helpful

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 Joein his answer:

此外,您应该先将 Integer 更改为 BigInteger,然后进行比较,如Joe他的回答中所述:

 Integer a=3;
 if(BigInteger.valueOf(a).compareTo(BigInteger.TEN)<0){
    // your code...
 }
 else{
    // your rest code, and so on.
 } 

回答by Mureinik

Just use BigInteger.compare:

只需使用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");
}