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:ttworkbench.play.parameters.ipv6.editors.octet.OctetRangeVerifier.java

private static Long valueToOctets(BigInteger theValue) {
    if (theValue == null || theValue.compareTo(new BigInteger("0")) <= 0)
        return 0L;

    double valueLn = Math.log(theValue.doubleValue()) / Math.log(2);
    long valueOctets = (long) Math.floor(valueLn / 8.0) + 1;
    return valueOctets;
}

From source file:com.web.vehiclerouting.optaplanner.common.persistence.AbstractSolutionImporter.java

public static String getFlooredPossibleSolutionSize(BigInteger possibleSolutionSize) {
    if (possibleSolutionSize.compareTo(BigInteger.valueOf(1000L)) < 0) {
        return possibleSolutionSize.toString();
    }/*from ww  w.  jav  a 2s . co m*/
    // TODO this is slow for machinereassingment's biggest dataset
    return "10^" + (possibleSolutionSize.toString().length() - 1);
}

From source file:org.optaplanner.examples.common.persistence.AbstractSolutionImporter.java

public static String getFlooredPossibleSolutionSize(BigInteger possibleSolutionSize) {
    if (possibleSolutionSize.compareTo(BigInteger.valueOf(1000L)) < 0) {
        return possibleSolutionSize.toString();
    }//w  ww. j  av a 2 s  .  co m
    return "10^" + (BigIntegerMath.log10(possibleSolutionSize, RoundingMode.FLOOR));
}

From source file:Main.java

private static boolean passesMillerRabin(BigInteger us, int iterations, Random rnd) {
    final BigInteger ONE = BigInteger.ONE;
    final BigInteger TWO = BigInteger.valueOf(2);
    // Find a and m such that m is odd and this == 1 + 2**a * m
    BigInteger thisMinusOne = us.subtract(ONE);
    BigInteger m = thisMinusOne;/*from   ww w.j a v  a  2  s .com*/
    int a = m.getLowestSetBit();
    m = m.shiftRight(a);

    // Do the tests
    if (rnd == null) {
        rnd = new SecureRandom();
    }
    for (int i = 0; i < iterations; i++) {
        // Generate a uniform random on (1, this)
        BigInteger b;
        do {
            b = new BigInteger(us.bitLength(), rnd);
        } while (b.compareTo(ONE) <= 0 || b.compareTo(us) >= 0);

        int j = 0;
        BigInteger z = b.modPow(m, us);
        while (!((j == 0 && z.equals(ONE)) || z.equals(thisMinusOne))) {
            if (j > 0 && z.equals(ONE) || ++j == a)
                return false;
            z = z.modPow(TWO, us);
        }
    }
    return true;
}

From source file:org.hyperledger.common.ByteUtils.java

/**
 * convert a byte array to a human readable base58 string. Base58 is a Bitcoin specific encoding similar to widely used base64 but avoids using characters
 * of similar shape, such as 1 and l or O an 0
 *
 * @param b/*  w  w  w  .  j a v a2  s.c  o m*/
 * @return
 */
public static String toBase58(byte[] b) {
    if (b.length == 0) {
        return "";
    }

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

    StringBuilder s = new StringBuilder();
    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:cc.redberry.core.number.NumberUtils.java

public static boolean isSqrt(BigInteger n, BigInteger root) {
    return n.compareTo(root.pow(2)) == 0;
}

From source file:com.nearinfinity.honeycomb.hbase.bulkload.FieldParser.java

/**
 * Try to parse a string into a byte string based on a column type.
 *
 * @param val    String value/*  w w  w  .j a v a  2s  . c  o m*/
 * @param schema Column schema to base value parsing on.
 * @return Byte string
 * @throws ParseException The string value could not be parsed into the column type.
 */
public static ByteBuffer parse(String val, ColumnSchema schema) throws ParseException {
    checkNotNull(val, "Should not be parsing null. Something went terribly wrong.");
    checkNotNull(schema, "Column metadata is null.");

    ColumnType type = schema.getType();

    if (val.length() == 0 && type != ColumnType.STRING && type != ColumnType.BINARY) {
        if (schema.getIsNullable()) {
            return null;
        }

        throw new IllegalArgumentException(
                "Expected a value for a" + " non-null SQL column, but no value was given.");
    }

    switch (type) {
    case LONG:
        return ByteBuffer.wrap(Longs.toByteArray(Long.parseLong(val)));
    case ULONG:
        BigInteger n = new BigInteger(val);
        if (n.compareTo(BigInteger.ZERO) == -1) {
            throw new IllegalArgumentException("negative value provided for unsigned column. value: " + val);
        }
        return ByteBuffer.wrap(Longs.toByteArray(n.longValue()));
    case DOUBLE:
        return ByteBuffer.wrap(Bytes.toBytes(Double.parseDouble(val)));
    case DATE:
        return extractDate(val, "yyyy-MM-dd", "yyyy-MM-dd", "yyyy/MM/dd", "yyyy.MM.dd", "yyyyMMdd");
    case TIME:
        return extractDate(val, "HH:mm:ss", "HH:mm:ss", "HHmmss");
    case DATETIME:
        return extractDate(val, "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm:ss", "yyyy/MM/dd HH:mm:ss",
                "yyyy.MM.dd HH:mm:ss", "yyyyMMdd HHmmss");
    case DECIMAL:
        return extractDecimal(val, schema.getPrecision(), schema.getScale());
    case STRING:
    case BINARY:
    default:
        return ByteBuffer.wrap(val.getBytes(Charset.forName("UTF-8")));
    }
}

From source file:Main.java

public static boolean isBetweenTimeStr(String time, String starttime, String endtime) {
    try {/*  ww  w . ja  va2s .c  om*/
        BigInteger timeInt = new BigInteger(time);
        BigInteger starttimeInt = new BigInteger(starttime);
        BigInteger endttimeInt = new BigInteger(endtime);
        int compareResult = timeInt.compareTo(starttimeInt);
        if (compareResult == -1) {
            return false;
        } else if (compareResult == 0) {
            return true;
        } else if (compareResult == 1) {
            compareResult = timeInt.compareTo(endttimeInt);
            if (compareResult >= 0) {
                return false;
            } else {
                return true;
            }
        }
        return false;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:edu.hku.sdb.udf.util.UDFHandler.java

/**
 * (1) value less than halfN means that a > b, return false
 * (2) value greater or equal to halfN means that a < b, return true
 * @param value/*from w w w .  j a v a 2  s  .  co m*/
 * @param halfN
 * @return
 */
public static boolean lessThan(BigInteger value, BigInteger halfN) {
    int result = value.compareTo(halfN);

    if (result < 0)
        return false;
    else
        return true;
}

From source file:Util.java

protected static BigInteger sqrt(BigInteger number, BigInteger guess) {
    // ((n/g) + g)/2: until same result twice in a row
    //      BigInteger result = number.divide(guess).add(guess).divide(BigIntegerTWO);
    //      if(result.compareTo(guess) == 0)
    //         return result;
    ///*ww w  .j  a v  a2 s  .c  o m*/
    //      return sqrt(number, result);

    // redoing this to avoid StackOverFlow
    BigInteger result = BigIntegerZERO;
    BigInteger flipA = result;
    BigInteger flipB = result;
    boolean first = true;
    while (result.compareTo(guess) != 0) {
        if (!first)
            guess = result;
        else
            first = false;

        result = number.divide(guess).add(guess).divide(BigIntegerTWO);
        // handle flip flops
        if (result.equals(flipB))
            return flipA;

        flipB = flipA;
        flipA = result;
    }
    return result;

}