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.
Peki, neden o çalışmadığını düşünüyorsunuz? –
@ E_net4 Um ... Neden çalışmadığını biliyorum. Bir çözüm arıyorum. – Progo
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