Example usage for java.math BigInteger equals

List of usage examples for java.math BigInteger equals

Introduction

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

Prototype

public boolean equals(Object x) 

Source Link

Document

Compares this BigInteger with the specified Object for equality.

Usage

From source file:Main.java

public static void main(String[] args) {

    BigInteger bi1 = new BigInteger("123");
    BigInteger bi2 = new BigInteger("123");

    // compare bi1 with bi2
    Boolean b1 = bi1.equals(bi2);

    // compare bi1 with an object value 123, which is not a BigIntger
    Boolean b2 = bi1.equals("123");

    System.out.println(b1);//from   w  w  w . j  av  a2s .  com
    System.out.println(b2);
}

From source file:RandomUtil.java

public static BigInteger randomBitFlip(BigInteger n) {
    if (n.equals(BigInteger.ONE))
        return BigInteger.ONE;
    if (n.equals(BigInteger.ZERO))
        return BigInteger.ZERO;

    BigInteger toRet = BigInteger.ONE;
    while (toRet.equals(BigInteger.ONE) || toRet.equals(BigInteger.ZERO)) {
        byte[] bytes = n.toByteArray();
        getInstance().random.nextBytes(bytes);
        // could allow up to maxExclusive by converting and checking value
        // but this is faster, even if it doesn't give us full range
        bytes[0] = 0;/*from   w ww  .ja  v  a 2s  . co m*/
        toRet = new BigInteger(bytes);
    }
    return toRet;
}

From source file:Util.java

public static BigInteger bitwiseGcd(BigInteger u, BigInteger v) {
    if (u.equals(BigInteger.ZERO))
        return v;
    if (v.equals(BigInteger.ZERO))
        return u;

    //      System.out.format("u=%s=%s\nu.getLowestSetBit()=%s\n%s>>%s=%s = %s\n", u, u.toString(2), u.getLowestSetBit(), u, u.getLowestSetBit(), u.shiftRight(u.getLowestSetBit()), u.shiftRight(u.getLowestSetBit()).toString(2));

    int uBit = u.getLowestSetBit();
    int vBit = v.getLowestSetBit();
    int k = (uBit <= vBit ? uBit : vBit);

    while (u.signum() > 0) {
        // first ensure that both are odd
        u = u.shiftRight(u.getLowestSetBit());
        v = v.shiftRight(v.getLowestSetBit());
        if (u.subtract(v).signum() >= 0) {
            u = (u.subtract(v)).shiftRight(1);
        } else {// w  w w .  j a v a  2  s  .  c o m
            v = (v.subtract(u)).shiftRight(1);
        }
    }

    return v.shiftLeft(k);
}

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   w  w  w .ja va2s.c  om*/
    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:Main.java

static BigInteger inverseMod(BigInteger a, BigInteger b) {
    BigInteger b0 = b, t, q;/*from w ww. j  ava  2s  . com*/
    BigInteger x0 = BigInteger.ZERO, x1 = BigInteger.ONE;
    if (b.equals(BigInteger.ONE))
        return BigInteger.ONE;
    while (a.subtract(BigInteger.ONE).signum() > 0) {
        q = a.divide(b);
        t = b;
        b = a.mod(b);
        a = t;
        t = x0;
        x0 = x1.subtract(q.multiply(x0));
        x1 = t;
    }
    if (x1.signum() < 0)
        x1 = x1.add(b0);
    return x1;
}

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

/**
 * Return true is value is zero./* w ww .j a  v  a 2  s.c  o m*/
 *
 * @param value
 * @return
 */
public static boolean equal(BigInteger value) {
    return value.equals(BigInteger.ZERO);
}

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

/**
 * (1) value less than halfN means that a > b, return true
 * (2) value greater or equal to halfN means that a < b, return false
 * @param value//  w  w w  .  ja  v  a2 s. c o  m
 * @param halfN
 * @return
 */
public static boolean greatThan(BigInteger value, BigInteger halfN) {
    if (value.equals(BigInteger.ZERO))
        return false;

    int result = value.compareTo(halfN);

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

From source file:Main.java

/**
 * Convert to base 128 (bigendian), using shifts.
 * @param val/*from   w  w  w .j av a 2  s . com*/
 * @return
 */
public static ArrayList<Integer> base128(BigInteger val) {
    ArrayList<Integer> result = new ArrayList<Integer>();
    int part = val.and(BN127).intValue();
    val = val.shiftRight(7);
    result.add(0, new Integer(part));
    while (!val.equals(BigInteger.ZERO)) {
        part = val.and(BN127).intValue();
        val = val.shiftRight(7);
        part += 128;
        result.add(0, new Integer(part));
    }
    ;
    return result;
}

From source file:Bytes.java

/**
 * Convert the specified amount into a human readable (though slightly less accurate)
 * result. IE://ww w  . j a  v a  2  s  . c  o m
 * '4096 B' to '4 KB'
 * '5080 B' to '5 KB' even though it is really '4 KB + 984 B'
 */
public static String friendly(Bytes type, BigInteger value) {
    /**
     * Logic:
     * Loop from YB to B
     * If result = 0, continue
     * Else, round off
     *
     * NOTE: BigIntegers are not reusable, so not point in caching them outside the loop
     */
    for (Bytes newType : reversed) {
        BigInteger newAmount = newType.convertFrom(value, type);
        if (newAmount.equals(BigInteger.ZERO))
            continue;
        // Found the right one. Now to round off
        BigInteger unitBytes = Bytes.B.convertFrom(BigInteger.ONE, newType);
        BigInteger usedBytes = newAmount.multiply(unitBytes);
        BigInteger remainingBytes = Bytes.B.convertFrom(value, type).subtract(usedBytes);
        if (remainingBytes.equals(BigInteger.ZERO))
            return String.format(friendlyFMT, newAmount.toString(), newType);
        if (remainingBytes.equals(value))
            return String.format(friendlyFMT, newAmount.toString(), newType);

        BigInteger halfUnit = unitBytes.divide(TWO);
        if ((remainingBytes.subtract(halfUnit)).signum() < 0)
            return String.format(friendlyFMT, newAmount.toString(), newType);

        return String.format(friendlyFMT, (newAmount.add(BigInteger.ONE)).toString(), newType);
    }

    // Give up
    return String.format(friendlyFMT, value.toString(), type);
}

From source file:org.eclipse.om2m.binding.coap.CoapServer.java

/**
 * Converts a {@link StatusCode} object into a standard CoAP status code .
 *
 * @param statusCode/*from   ww  w .  j  ava2 s .  c  om*/
 *            - protocol-independent status code.
 * @param isEmptyBody
 *            - request body existence
 * @return standard CoAP status code.
 */
public static ResponseCode getCoapStatusCode(BigInteger statusCode) {
    if (statusCode.equals(ResponseStatusCode.OK)) {
        return ResponseCode.CONTENT;
    } else if (statusCode.equals(ResponseStatusCode.CREATED)) {
        return ResponseCode.CREATED;
    } else if (statusCode.equals(ResponseStatusCode.ACCEPTED)) {
        return ResponseCode.VALID;
    } else if (statusCode.equals(ResponseStatusCode.DELETED)) {
        return ResponseCode.DELETED;
    } else if (statusCode.equals(ResponseStatusCode.UPDATED)) {
        return ResponseCode.CHANGED;
    } else if (statusCode.equals(ResponseStatusCode.BAD_REQUEST)
            || statusCode.equals(ResponseStatusCode.CONTENTS_UNACCEPTABLE)
            || statusCode.equals(ResponseStatusCode.GROUP_REQUEST_IDENTIFIER_EXISTS)
            || statusCode.equals(ResponseStatusCode.ALREADY_EXISTS)
            || statusCode.equals(ResponseStatusCode.MAX_NUMBER_OF_MEMBER_EXCEEDED)
            || statusCode.equals(ResponseStatusCode.MEMBER_TYPE_INCONSISTENT)
            || statusCode.equals(ResponseStatusCode.INVALID_CMDTYPE)
            || statusCode.equals(ResponseStatusCode.INVALID_ARGUMENTS)
            || statusCode.equals(ResponseStatusCode.ALREADY_COMPLETED)
            || statusCode.equals(ResponseStatusCode.COMMAND_NOT_CANCELLABLE)) {
        return ResponseCode.BAD_REQUEST;
    } else if (statusCode.equals(ResponseStatusCode.ACCESS_DENIED)
            || statusCode.equals(ResponseStatusCode.SUBSCRIPTION_CREATOR_HAS_NO_PRIVILEGE)
            || statusCode.equals(ResponseStatusCode.NO_PRIVILEGE)
            || statusCode.equals(ResponseStatusCode.ALREADY_EXISTS)
            || statusCode.equals(ResponseStatusCode.CONFLICT)
            || statusCode.equals(ResponseStatusCode.TARGET_NOT_SUBSCRIBABLE)
            || statusCode.equals(ResponseStatusCode.SUBSCRIPTION_HOST_HAS_NO_PRIVILEGE)) {
        return ResponseCode.FORBIDDEN;
    } else if (statusCode.equals(ResponseStatusCode.NOT_FOUND)
            || statusCode.equals(ResponseStatusCode.REQUEST_TIMEOUT)
            || statusCode.equals(ResponseStatusCode.TARGET_NOT_REACHABLE)
            || statusCode.equals(ResponseStatusCode.EXTERNAL_OBJECT_NOT_FOUND)
            || statusCode.equals(ResponseStatusCode.EXTERNAL_OBJECT_NOT_REACHABLE)) {
        return ResponseCode.NOT_FOUND;
    } else if (statusCode.equals(ResponseStatusCode.OPERATION_NOT_ALLOWED)) {
        return ResponseCode.METHOD_NOT_ALLOWED;
    } else if (statusCode.equals(ResponseStatusCode.REQUEST_TIMEOUT)) {
        return ResponseCode.SERVICE_UNAVAILABLE;
    } else if (statusCode.equals(ResponseStatusCode.CONFLICT)
            || statusCode.equals(ResponseStatusCode.GROUP_REQUEST_IDENTIFIER_EXISTS)) {
        return ResponseCode.INTERNAL_SERVER_ERROR;
    } else if (statusCode.equals(ResponseStatusCode.INTERNAL_SERVER_ERROR)
            || statusCode.equals(ResponseStatusCode.SUBSCRIPTION_VERIFICATION_INITIATION_FAILED)
            || statusCode.equals(ResponseStatusCode.MGMT_SESSION_CANNOT_BE_ESTABLISHED)
            || statusCode.equals(ResponseStatusCode.MGMT_SESSION_ESTABLISHMENT_TIMEOUT)
            || statusCode.equals(ResponseStatusCode.MGMT_CONVERSION_ERROR)
            || statusCode.equals(ResponseStatusCode.MGMT_CANCELATION_FAILURE)) {
        return ResponseCode.INTERNAL_SERVER_ERROR;
    } else if (statusCode.equals(ResponseStatusCode.NOT_IMPLEMENTED)
            || statusCode.equals(ResponseStatusCode.NON_BLOCKING_REQUEST_NOT_SUPPORTED)) {
        return ResponseCode.NOT_IMPLEMENTED;
    } else if (statusCode.equals(ResponseStatusCode.SERVICE_UNAVAILABLE)) {
        return ResponseCode.SERVICE_UNAVAILABLE;
    }
    return ResponseCode.SERVICE_UNAVAILABLE;
}