2014-09-13 16 views
6

मैं जावा में BigInteger के साथ int की तुलना कैसे करूं? मुझे विशेष रूप से पता होना चाहिए कि intBigInteger से कम है या नहीं। यहां मैं कोड का उपयोग कर रहा हूं:जावा पूर्णांक की तुलना करें और 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; 
}; 

धन्यवाद।

+0

ठीक है, तुम क्यों लगता है कि नहीं है काम कर रहे हैं? –

+0

@ ई_नेट 4 उम ... मुझे पता है कि यह क्यों काम नहीं कर रहा है। मैं एक समाधान की तलाश में हूँ। – Progo

+2

यदि आप जो पूछ रहे हैं वह बहुत सी कोड है "एक इंट के साथ BigInt की तुलना करें"। क्या वहां कोई और प्रश्न छिपा हुआ है? अन्यथा: http://docs.oracle.com/javase/6/docs/api/java/math/BigInteger.html#compareTo(java.math.BigInteger) 'तुलना करने के लिए' रिटर्न -1 (इससे कम), 0 (बराबर), या 1 (0 से अधिक) – Gus

उत्तर

16

मैं जावा में बिगइंटर के साथ int की तुलना कैसे करूं? मुझे विशेष रूप से पता होना चाहिए कि कोई int बिगइंटर से कम है या नहीं।

की तुलना से पहले दाएं मुड़ें एक BigInteger में int:

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

+1 को बिगइन्ट में int कास्टिंग करने के लिए +1, दूसरी तरफ बनाम। शायद उल्लेख करना चाहें क्यों – Gus

4

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

के बजाय आप का उपयोग करना चाहिए: -

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

इसके अलावा, आप पूर्णांक के रूप में अपने जवाब में Joe से उल्लेख किया है, BigInteger करने के लिए पहले बदलना चाहिए, और फिर तुलना:

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

हालांकि यह प्रश्न के कोड स्निपेट में एक मुद्दा है, जो वास्तव में मुख्य प्रश्न का उत्तर नहीं देता है। –

1

बस 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"); 
} 
संबंधित मुद्दे