2014-09-13 32 views
6

Bir int'u BigInteger ile Java'da nasıl karşılaştırabilirim? Bir int'un BigInteger'dan daha az olup olmadığını bilhassa bilmem gerekir. İşte kullanıyorum kod:Java karşılaştırması tamsayı ve 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; 
}; 

Teşekkürler.

+0

Peki, neden o çalışmadığını düşünüyorsunuz? –

+0

@ E_net4 Um ... Neden çalışmadığını biliyorum. Bir çözüm arıyorum. – Progo

+2

Bu sizin için sorduğunuz şey "BigInt'i bir int ile karşılaştır" ise çok fazla bir kod. Orada gizli bir soru var mı? Aksi takdirde: http://docs.oracle.com/javase/6/docs/api/java/math/BigInteger.html#compareTo(java.math.BigInteger) 'compareTo' -1 (az), 0 (eşit) döndürür) veya 1 (büyük) – Gus

cevap

16

Nasıl Java bir BigInteger ile bir int karşılaştırılır? Bir int'nin bir BigInteger'den daha az olup olmadığını bilmem gerekiyor.

karşılaştırarak önce BigInteger içine int çevirin

:

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

+1, int'nin bir BigInt'e dönüştürülmesi için, diğer yöne göre. Nedenini belirtmek isteyebilir – Gus

4

yerine

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

Seni kullanmalıdır: - Ayrıca

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

, onun cevabını Joe tarafından belirtildiği gibi, karşılaştırmak sonra BigInteger ilk Integer değiştirin ve olmalıdır:

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

Sorunun kod snippet'inde bir sorun olsa da, asıl soruya gerçekten yanıt vermez. –

1

Yalnızca BigInteger.compare kullanın:

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"); 
} 
İlgili konular