Example usage for java.math BigInteger add

List of usage examples for java.math BigInteger add

Introduction

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

Prototype

BigInteger add(long val) 

Source Link

Document

Package private methods used by BigDecimal code to add a BigInteger with a long.

Usage

From source file:BigNumApp.java

public static void main(String args[]) {
    BigInteger n = new BigInteger("1000000000000");
    BigInteger one = new BigInteger("1");
    while (!n.isProbablePrime(7))
        n = n.add(one);
    System.out.println(n.toString(10) + " is probably prime.");
    System.out.println("It is " + n.bitLength() + " bits in length.");
}

From source file:Main.java

public static void main(String[] args) {
    BigInteger numberA = new BigInteger("98765432123456789");
    BigInteger numberB = BigInteger.TEN;

    numberA = numberA.add(numberB);
    System.out.println("numberA = " + numberA);

    numberA = numberA.multiply(numberB);
    System.out.println("numberA = " + numberA);

    numberA = numberA.subtract(numberB);
    System.out.println("numberA = " + numberA);

    numberA = numberA.divide(numberB);//w  w w  .  j a  v a  2s.  c  o m
    System.out.println("numberA = " + numberA);

    numberA = numberA.mod(numberB);
    System.out.println("numberA = " + numberA);

    numberA = numberA.pow(2);
    System.out.println("numberA = " + numberA);

    numberA = numberA.negate();
    System.out.println("numberA = " + numberA);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    // Create via a string
    BigInteger bi1 = new BigInteger("1234567890123456890");

    // Create via a long
    BigInteger bi2 = BigInteger.valueOf(123L);

    bi1 = bi1.add(bi2);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    // Create via a string
    BigInteger bi1 = new BigInteger("1234567890123456890");

    // Create via a long
    BigInteger bi2 = BigInteger.valueOf(123L);

    bi1 = bi1.add(bi2);
    bi1 = bi1.multiply(bi2);//from  w  w w . ja  v a 2  s.co m
    bi1 = bi1.subtract(bi2);
    bi1 = bi1.divide(bi2);
    bi1 = bi1.negate();
    int exponent = 2;
    bi1 = bi1.pow(exponent);

}

From source file:Main.java

public static void main(String[] args) {

    // assign values to bi1, bi2
    BigInteger bi1 = new BigInteger("123");
    BigInteger bi2 = new BigInteger("50");

    // perform add operation on bi1 using bi2
    BigInteger bi3 = bi1.add(bi2);

    System.out.println(bi3);//ww  w  .  j  a v a2 s .  co m
}

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:Main.java

public static Date toDate(Instant instant) {
    BigInteger milis = BigInteger.valueOf(instant.getEpochSecond()).multiply(BigInteger.valueOf(1000));
    milis = milis.add(BigInteger.valueOf(instant.getNano()).divide(BigInteger.valueOf(1_000_000)));
    return new Date(milis.longValue());
}

From source file:Main.java

public static BigInteger sumOf(Collection<Integer> numbers) {
    BigInteger sum = BigInteger.ZERO;
    for (Integer n : numbers) {
        sum = sum.add(BigInteger.valueOf(n));
    }//from w  w w. ja  va  2s. com
    return sum;
}

From source file:Main.java

static BigInteger addmod(BigInteger a, BigInteger b, BigInteger p) {
    if (p.subtract(b).compareTo(a) > 0) {
        return a.add(b);
    } else {/*ww  w  .j a  v a  2s .co  m*/
        return a.add(b).subtract(p);
    }
}

From source file:Main.java

public static BigInteger sqrt(final BigInteger val) {
    final BigInteger two = BigInteger.valueOf(2);
    BigInteger a = BigInteger.ONE.shiftLeft(val.bitLength() / 2);
    BigInteger b;/*from w  w  w . j  av  a 2 s  . c  om*/
    do {
        b = val.divide(a);
        a = (a.add(b)).divide(two);
    } while (a.subtract(b).abs().compareTo(two) >= 0);
    return a;
}