Example usage for java.math BigInteger compareTo

List of usage examples for java.math BigInteger compareTo

Introduction

In this page you can find the example usage for java.math BigInteger compareTo.

Prototype

public int compareTo(BigInteger val) 

Source Link

Document

Compares this BigInteger with the specified BigInteger.

Usage

From source file:Main.java

public static void main(String[] args) {
    BigInteger bigInteger = new BigInteger("2000000000000");// uper limit
    BigInteger min = new BigInteger("1000000000");// lower limit
    BigInteger bigInteger1 = bigInteger.subtract(min);
    Random rnd = new Random();
    int maxNumBitLength = bigInteger.bitLength();

    BigInteger aRandomBigInt;

    aRandomBigInt = new BigInteger(maxNumBitLength, rnd);
    if (aRandomBigInt.compareTo(min) < 0)
        aRandomBigInt = aRandomBigInt.add(min);
    if (aRandomBigInt.compareTo(bigInteger) >= 0)
        aRandomBigInt = aRandomBigInt.mod(bigInteger1).add(min);

    System.out.println(aRandomBigInt);
}

From source file:PalindromeBig.java

public static void main(String[] argv) {
    for (int i = 0; i < argv.length; i++)
        try {//from w  w w .  ja va2  s  .c o  m
            BigInteger l = new BigInteger(argv[i]);
            if (l.compareTo(BigInteger.ZERO) < 0) {
                System.err.println(argv[i] + " -> TOO SMALL");
                continue;
            }
            System.out.println(argv[i] + "->" + findPalindrome(l));
        } catch (NumberFormatException e) {
            System.err.println(argv[i] + "-> INVALID");
        } catch (IllegalStateException e) {
            System.err.println(argv[i] + "-> " + e);
        }
}

From source file:Main.java

public static void main(String[] args) {

    BigInteger bi1 = new BigInteger("6");
    BigInteger bi2 = new BigInteger("3");

    // compare bi1 with bi2
    int res = bi1.compareTo(bi2);

    String str1 = "Both values are equal ";
    String str2 = "First Value is greater ";
    String str3 = "Second value is greater";

    if (res == 0)
        System.out.println(str1);
    else if (res == 1)
        System.out.println(str2);
    else if (res == -1)
        System.out.println(str3);
}

From source file:Main.java

/** find a palindromic number given a starting point, by
 * calling ourself until we get a number that is palindromic.
 *///from   ww w  . j  a  va  2s .com
public static BigInteger findPalindrome(BigInteger num) {
    if (num.compareTo(BigInteger.ZERO) < 0)
        throw new IllegalStateException("negative");
    if (isPalindrome(num))
        return num;
    if (verbose)
        System.out.println("Trying " + num);
    return findPalindrome(num.add(reverseNumber(num)));
}

From source file:Main.java

public static boolean isGoodGaAndGb(BigInteger g_a, BigInteger p) {
    return !(g_a.compareTo(BigInteger.valueOf(1)) != 1
            || g_a.compareTo(p.subtract(BigInteger.valueOf(1))) != -1);
}

From source file:Main.java

public static BigInteger modNear(BigInteger a, BigInteger b) {
    BigInteger res = a.mod(b);

    if (res.compareTo(b.shiftRight(1)) == 1)
        res = res.subtract(b);/*  ww  w.j ava2s .c o m*/

    return res;
}

From source file:Main.java

public static String getValueShortString(BigInteger number) {
    BigInteger result = number;
    int pow = 0;//from  w  w w. ja v a 2 s . co m
    while (result.compareTo(_1000_) == 1 || result.compareTo(_1000_) == 0) {
        result = result.divide(_1000_);
        pow += 3;
    }
    return result.toString() + "\u00b7(" + "10^" + pow + ")";
}

From source file:Main.java

public static String toBase58(byte[] b) {
    if (b.length == 0) {
        return "";
    }//from  ww w.ja v a 2  s . com

    int lz = 0;
    while (lz < b.length && b[lz] == 0) {
        ++lz;
    }

    StringBuffer s = new StringBuffer();
    BigInteger n = new BigInteger(1, b);
    while (n.compareTo(BigInteger.ZERO) > 0) {
        BigInteger[] r = n.divideAndRemainder(BigInteger.valueOf(58));
        n = r[0];
        char digit = b58[r[1].intValue()];
        s.append(digit);
    }
    while (lz > 0) {
        --lz;
        s.append("1");
    }
    return s.reverse().toString();
}

From source file:Main.java

public static int compareTimestamps(String lhsTimestamp, String rhsTimestamp) {
    BigInteger lhs = new BigInteger(lhsTimestamp);
    BigInteger rhs = new BigInteger(rhsTimestamp);
    return lhs.compareTo(rhs);
}

From source file:Main.java

static BigInteger mulmod(BigInteger a, BigInteger b, BigInteger p) {
    BigInteger r = BigInteger.ZERO;
    while (b.compareTo(BigInteger.ZERO) > 0) {
        if (!b.and(BigInteger.ONE).equals(BigInteger.ZERO)) {
            r = addmod(r, a, p);//from ww w  .j a v a 2 s. c  o  m
        }
        b = b.shiftRight(1);
        a = addmod(a, a, p);
    }
    return r;
}