Example usage for java.math BigInteger subtract

List of usage examples for java.math BigInteger subtract

Introduction

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

Prototype

public BigInteger subtract(BigInteger val) 

Source Link

Document

Returns a BigInteger whose value is (this - val) .

Usage

From source file:RSA.java

/** Generate a new public and private key set. */
public synchronized void generateKeys() {
    SecureRandom r = new SecureRandom();
    BigInteger p = new BigInteger(bitlen / 2, 100, r);
    BigInteger q = new BigInteger(bitlen / 2, 100, r);
    n = p.multiply(q);//from  w ww.  j  a  va2 s. com
    BigInteger m = (p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE));
    e = new BigInteger("3");
    while (m.gcd(e).intValue() > 1) {
        e = e.add(new BigInteger("2"));
    }
    d = e.modInverse(m);
}

From source file:RSA.java

/** Create an instance that can both encrypt and decrypt. */
public RSA(int bits) {
    bitlen = bits;/*  w  w  w.  java  2 s .c  om*/
    SecureRandom r = new SecureRandom();
    BigInteger p = new BigInteger(bitlen / 2, 100, r);
    BigInteger q = new BigInteger(bitlen / 2, 100, r);
    n = p.multiply(q);
    BigInteger m = (p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE));
    e = new BigInteger("3");
    while (m.gcd(e).intValue() > 1) {
        e = e.add(new BigInteger("2"));
    }
    d = e.modInverse(m);
}

From source file:com.google.uzaygezen.core.ranges.BigIntegerRange.java

BigInteger overlap(BigIntegerRange other) {
    if (start.compareTo(other.getEnd()) >= 0 || end.compareTo(other.getStart()) <= 0) {
        return BigInteger.ZERO;
    } else {//  w  w  w  .  j  a  v  a2s .c  o m
        if (start.compareTo(other.getStart()) < 0 || end.compareTo(other.getEnd()) > 0) {
            BigInteger x = getEnd().min(other.getEnd());
            BigInteger y = getStart().max(other.getStart());
            return x.subtract(y);
        } else {
            return end.subtract(start);
        }
    }
}

From source file:net.sf.jasperreports.data.cache.BigIntegerStore.java

protected ColumnValues createPrimitiveValues(boolean useOffset) {
    if (primitiveStore == null) {
        primitiveStore = new LongArrayStore(rawStore.size(), true);
    } else {/*from ww w  . j a v a 2  s .co  m*/
        primitiveStore.resetValues();
    }

    int count = rawStore.count();
    Object[] values = rawStore.valuesBuffer();
    for (int i = 0; i < count; i++) {
        BigInteger value = (BigInteger) values[i];
        if (useOffset) {
            value = value.subtract(min);
        }
        primitiveStore.add(value.longValue());
    }

    ColumnValues primitiveValues = primitiveStore.createValues();
    return primitiveValues;
}

From source file:libra.common.kmermatch.KmerJoiner.java

public float getProgress() {
    if (this.progressKey == null) {
        if (this.eof) {
            return 1.0f;
        } else {/*from w  w w . j a  v a2  s.  c  o m*/
            return 0.0f;
        }
    } else {
        BigInteger seq = SequenceHelper.convertToBigInteger(this.progressKey.getSequence());
        BigInteger prog = seq.subtract(this.beginSequence);
        int comp = this.partitionSize.compareTo(prog);
        if (comp <= 0) {
            return 1.0f;
        } else {
            BigDecimal progDecimal = new BigDecimal(prog);
            BigDecimal rate = progDecimal.divide(new BigDecimal(this.partitionSize), 3,
                    BigDecimal.ROUND_HALF_UP);

            float f = rate.floatValue();
            return Math.min(1.0f, f);
        }
    }
}

From source file:org.apache.hama.util.Bytes.java

/**
 * Split passed range. Expensive operation relatively. Uses BigInteger math.
 * //  w  ww.  j a v a2  s . c  o m
 * @param a Beginning of range
 * @param b End of range
 * @param num Number of times to split range. Pass 1 if you want to split the
 *          range in two; i.e. one split.
 * @return Array of dividing values
 */
public static byte[][] split(final byte[] a, final byte[] b, final int num) {
    byte[] aPadded;
    byte[] bPadded;
    if (a.length < b.length) {
        aPadded = padTail(a, b.length - a.length);
        bPadded = b;
    } else if (b.length < a.length) {
        aPadded = a;
        bPadded = padTail(b, a.length - b.length);
    } else {
        aPadded = a;
        bPadded = b;
    }
    if (compareTo(aPadded, bPadded) >= 0) {
        throw new IllegalArgumentException("b <= a");
    }
    if (num <= 0) {
        throw new IllegalArgumentException("num cannot be < 0");
    }
    byte[] prependHeader = { 1, 0 };
    BigInteger startBI = new BigInteger(add(prependHeader, aPadded));
    BigInteger stopBI = new BigInteger(add(prependHeader, bPadded));
    BigInteger diffBI = stopBI.subtract(startBI);
    BigInteger splitsBI = BigInteger.valueOf(num + 1);
    if (diffBI.compareTo(splitsBI) < 0) {
        return null;
    }
    BigInteger intervalBI;
    try {
        intervalBI = diffBI.divide(splitsBI);
    } catch (Exception e) {
        LOG.error("Exception caught during division", e);
        return null;
    }

    byte[][] result = new byte[num + 2][];
    result[0] = a;

    for (int i = 1; i <= num; i++) {
        BigInteger curBI = startBI.add(intervalBI.multiply(BigInteger.valueOf(i)));
        byte[] padded = curBI.toByteArray();
        if (padded[1] == 0)
            padded = tail(padded, padded.length - 2);
        else
            padded = tail(padded, padded.length - 1);
        result[i] = padded;
    }
    result[num + 1] = b;
    return result;
}

From source file:org.techytax.digipoort.XbrlNtp8Helper.java

public void addBalanceData(VatDeclarationData vatDeclarationData, VatBalanceWithinEu vatBalanceWithinEu)
        throws Exception {
    BigInteger totaleKosten = AmountHelper.roundToInteger(vatBalanceWithinEu.getTotaleKosten());
    BigInteger correction = AmountHelper.roundToInteger(vatBalanceWithinEu.getCorrection());
    BigInteger turnover = vatBalanceWithinEu.getNettoOmzet();
    BigInteger totaleBaten = AmountHelper.roundDownToInteger(
            new BigDecimal(turnover).multiply(BigDecimal.valueOf(VatType.HIGH.getValue(new Date()))));
    BigInteger vatOutEu = vatBalanceWithinEu.getVatOutEu();
    BigInteger owed = totaleBaten.add(correction).add(vatOutEu);
    BigInteger owedToBePaidBack = owed.subtract(totaleKosten.add(vatOutEu));
    vatDeclarationData.setValueAddedTaxOwed(owed);
    vatDeclarationData.setValueAddedTaxOnInput(totaleKosten.add(vatOutEu));
    vatDeclarationData.setValueAddedTaxOwedToBePaidBack(owedToBePaidBack);
    vatDeclarationData.setValueAddedTaxPrivateUse(correction);
    vatDeclarationData.setValueAddedTaxSuppliesServicesGeneralTariff(totaleBaten);
    vatDeclarationData.setTaxedTurnoverSuppliesServicesGeneralTariff(turnover);
    vatDeclarationData/*from  www.j  ava 2 s.  c o  m*/
            .setTurnoverFromTaxedSuppliesFromCountriesWithinTheEC(vatBalanceWithinEu.getTurnoverNetEu());
    vatDeclarationData.setValueAddedTaxOnSuppliesFromCountriesWithinTheEC(vatOutEu);
}

From source file:com.twitter.elephantbird.pig.load.HBaseSlice.java

@Override
public float getProgress() throws IOException {

    // No way to know max.. just return 0. Sorry, reporting on the last slice is janky.
    // So is reporting on the first slice, by the way -- it will start out too high, possibly at 100%.
    if (endRow_.length == 0)
        return 0;
    byte[] lastPadded = m_lastRow_;
    if (m_lastRow_.length < endRow_.length) {
        lastPadded = Bytes.padTail(m_lastRow_, endRow_.length - m_lastRow_.length);
    }//from  w ww .  jav  a 2s. c om
    if (m_lastRow_.length < startRow_.length) {
        lastPadded = Bytes.padTail(m_lastRow_, startRow_.length - m_lastRow_.length);
    }
    byte[] prependHeader = { 1, 0 };
    BigInteger bigLastRow = new BigInteger(Bytes.add(prependHeader, lastPadded));
    BigDecimal processed = new BigDecimal(bigLastRow.subtract(bigStart_));
    try {
        BigDecimal progress = processed.setScale(3).divide(bigRange_, BigDecimal.ROUND_HALF_DOWN);
        return progress.floatValue();
    } catch (java.lang.ArithmeticException e) {
        return 0;
    }
}

From source file:org.apache.accumulo.pig.Bytes.java

/**
 * Iterate over keys within the passed inclusive range.
 *//*from  w w w .ja v  a 2  s  .  co m*/
public static Iterable<byte[]> iterateOnSplits(final byte[] a, final byte[] b, final int num) {
    byte[] aPadded;
    byte[] bPadded;
    if (a.length < b.length) {
        aPadded = padTail(a, b.length - a.length);
        bPadded = b;
    } else if (b.length < a.length) {
        aPadded = a;
        bPadded = padTail(b, a.length - b.length);
    } else {
        aPadded = a;
        bPadded = b;
    }
    if (compareTo(aPadded, bPadded) >= 0) {
        throw new IllegalArgumentException("b <= a");
    }
    if (num <= 0) {
        throw new IllegalArgumentException("num cannot be < 0");
    }
    byte[] prependHeader = { 1, 0 };
    final BigInteger startBI = new BigInteger(add(prependHeader, aPadded));
    final BigInteger stopBI = new BigInteger(add(prependHeader, bPadded));
    final BigInteger diffBI = stopBI.subtract(startBI);
    final BigInteger splitsBI = BigInteger.valueOf(num + 1);
    if (diffBI.compareTo(splitsBI) < 0) {
        return null;
    }
    final BigInteger intervalBI;
    try {
        intervalBI = diffBI.divide(splitsBI);
    } catch (Exception e) {
        LOG.error("Exception caught during division", e);
        return null;
    }

    final Iterator<byte[]> iterator = new Iterator<byte[]>() {
        private int i = -1;

        @Override
        public boolean hasNext() {
            return i < num + 1;
        }

        @Override
        public byte[] next() {
            i++;
            if (i == 0) {
                return a;
            }
            if (i == num + 1) {
                return b;
            }

            BigInteger curBI = startBI.add(intervalBI.multiply(BigInteger.valueOf(i)));
            byte[] padded = curBI.toByteArray();
            if (padded[1] == 0) {
                padded = tail(padded, padded.length - 2);
            } else {
                padded = tail(padded, padded.length - 1);
            }
            return padded;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    };

    return new Iterable<byte[]>() {
        @Override
        public Iterator<byte[]> iterator() {
            return iterator;
        }
    };
}

From source file:localSPs.BearDataShareAPI.java

public double getStorageSize() throws IOException {
    BigInteger num = new BigInteger("1024");
    String ans = "";
    BigInteger totalMemory = new BigInteger("21474836480");
    BigInteger used;/*from www .  j av  a 2  s  .c o  m*/
    File dir = new File(ROOT_PATH);
    long totalUsed = 0; // total bytes
    List<File> files = (List<File>) FileUtils.listFiles(dir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
    for (File file : files) {
        totalUsed += file.length();
    }
    used = new BigInteger(String.valueOf(totalUsed));
    ans = totalMemory.subtract(used).divide(num).divide(num).toString();//MB
    return (Double.parseDouble(ans));
}