Example usage for java.math BigInteger multiply

List of usage examples for java.math BigInteger multiply

Introduction

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

Prototype

BigInteger multiply(long v) 

Source Link

Document

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

Usage

From source file:com.aegiswallet.utils.WalletUtils.java

public static BigInteger btcValue(Context context, SharedPreferences prefs,
        @Nonnull final BigInteger localValue) {
    BigInteger result = null;// w  w  w. j a  va 2 s.co m

    BigDecimal exchangeRate = getExchangeRate(context, prefs);
    if (exchangeRate != null)
        result = localValue.multiply(ONE_BTC).divide(exchangeRate.toBigInteger());

    return result;
}

From source file:com.trsst.Common.java

public static byte[] fromBase58(String s) {
    try {//from w  ww .ja  va2s  . c o m
        boolean leading = true;
        int lz = 0;
        BigInteger b = BigInteger.ZERO;
        for (char c : s.toCharArray()) {
            if (leading && c == '1') {
                ++lz;
            } else {
                leading = false;
                b = b.multiply(BigInteger.valueOf(58));
                b = b.add(BigInteger.valueOf(r58[c]));
            }
        }
        byte[] encoded = b.toByteArray();
        if (encoded[0] == 0) {
            if (lz > 0) {
                --lz;
            } else {
                byte[] e = new byte[encoded.length - 1];
                System.arraycopy(encoded, 1, e, 0, e.length);
                encoded = e;
            }
        }
        byte[] result = new byte[encoded.length + lz];
        System.arraycopy(encoded, 0, result, lz, encoded.length);

        return result;
    } catch (ArrayIndexOutOfBoundsException e) {
        throw new IllegalArgumentException("Invalid character in address");
    } catch (Exception e) {
        throw new IllegalArgumentException(e);
    }
}

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);
    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"));
    }/*from  w  ww .j  a  v  a2s  .c  o  m*/
    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  .j  a v a2 s .com*/
    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:jp.co.acroquest.endosnipe.report.converter.util.calc.BigIntegerCalculator.java

public Object mul(Object obj1, Object obj2) {
    BigInteger integerData1 = (BigInteger) obj1;
    BigInteger integerData2 = (BigInteger) obj2;

    return (Object) (integerData1.multiply(integerData2));
}

From source file:org.eclipse.equinox.p2.cudf.solver.TrendyOptimizationFunction.java

public List<WeightedObject<Object>> createOptimizationFunction(InstallableUnit metaIu) {
    List<WeightedObject<Object>> weightedObjects = new ArrayList<WeightedObject<Object>>();
    BigInteger weight = BigInteger.valueOf(slice.size() + 1);
    removed(weightedObjects, weight.multiply(weight).multiply(weight), metaIu);
    notuptodate(weightedObjects, weight.multiply(weight), metaIu);
    optional(weightedObjects, weight, metaIu);
    niou(weightedObjects, BigInteger.ONE, metaIu);
    if (!weightedObjects.isEmpty()) {
        return weightedObjects;
    }//from w  w w.  j a va 2s  . co  m
    return null;
}

From source file:de.tudarmstadt.ukp.teaching.uima.nounDecompounding.ranking.FrequencyBased.java

/**
 * Calculates the weight for a split//from  w  w w  .  j a va  2s.  c  om
 * @param split
 * @return
 */
private float calcRank(Split split) {
    BigInteger result = new BigInteger("1");

    for (SplitElement elem : split.getSplits()) {
        result = result.multiply(this.freq(elem));
    }

    return (float) Math.pow(result.doubleValue(), 1f / (double) split.getSplits().size());
}

From source file:org.apache.whirr.service.cassandra.CassandraClusterActionHandler.java

/**
 * Compute initial_token for a balanced cluster
 *///from   ww  w  .  j a  va  2 s  . co m
protected List<String> computeInitialTokens(int numberOfNodes) {
    List<String> tokens = Lists.newArrayList();

    BigInteger step = new BigInteger("2").pow(127).divide(BigInteger.valueOf(numberOfNodes));

    for (int i = 0; i < numberOfNodes; i++) {
        tokens.add(step.multiply(BigInteger.valueOf(i)).toString());
    }

    return tokens;
}

From source file:org.opendaylight.sfc.util.openflow.SfcOpenflowUtilsTest.java

public Object[][] bigIntegerToMacConversionsParams() {
    final BigInteger number256 = new BigInteger("256");
    final BigInteger MAX_MAC = number256.multiply(number256).multiply(number256).multiply(number256)
            .multiply(number256).multiply(number256).subtract(new BigInteger("1"));

    return new Object[][] { { new BigInteger("0"), "00:00:00:00:00:00" },
            { new BigInteger("1"), "00:00:00:00:00:01" }, { new BigInteger("15"), "00:00:00:00:00:0f" },
            { new BigInteger("16"), "00:00:00:00:00:10" }, { new BigInteger("17"), "00:00:00:00:00:11" },
            { new BigInteger("255"), "00:00:00:00:00:ff" }, { new BigInteger("256"), "00:00:00:00:01:00" },
            { new BigInteger("257"), "00:00:00:00:01:01" },
            { new BigInteger(new Integer(256 * 256).toString()), "00:00:00:01:00:00" },
            { MAX_MAC, "ff:ff:ff:ff:ff:ff" } };
}

From source file:org.limewire.mojito.util.DHTSizeEstimator.java

/**
 * Computes and returns the approximate DHT size based 
 * on the given List of Contacts./*www. j  av a 2  s . c o  m*/
 */
public synchronized BigInteger computeSize(Collection<? extends Contact> nodes) {

    // Works only with more than two Nodes
    if (nodes.size() < MIN_NODE_COUNT) {
        // There's always us!
        return BigInteger.ONE.max(BigInteger.valueOf(nodes.size()));
    }

    // Get the Iterator. We assume the Contacts are sorted by
    // their xor distance!
    Iterator<? extends Contact> contacts = nodes.iterator();

    // See Azureus DHTControlImpl.estimateDHTSize()
    // Di = nearestId xor NodeIDi
    // Dc = sum(i * Di) / sum(i * i)
    // Size = 2**160 / Dc

    BigInteger sum1 = BigInteger.ZERO;
    BigInteger sum2 = BigInteger.ZERO;

    // The algorithm works relative to the ID space.
    KUID nearestId = contacts.next().getNodeID();

    // We start 1 because the nearest Node is the 0th item!
    for (int i = 1; contacts.hasNext(); i++) {
        Contact node = contacts.next();

        BigInteger distance = nearestId.xor(node.getNodeID()).toBigInteger();
        BigInteger j = BigInteger.valueOf(i);

        sum1 = sum1.add(j.multiply(distance));
        sum2 = sum2.add(j.pow(2));
    }

    BigInteger estimatedSize = BigInteger.ZERO;
    if (!sum1.equals(BigInteger.ZERO)) {
        estimatedSize = KUID.MAXIMUM.toBigInteger().multiply(sum2).divide(sum1);
    }

    // And there is always us!
    estimatedSize = BigInteger.ONE.max(estimatedSize);

    // Get the average of the local estimations
    BigInteger localSize = BigInteger.ZERO;
    localSizeHistory.add(estimatedSize);

    // Adjust the size of the List. The Setting is SIMPP-able
    // and may change!
    int maxLocalHistorySize = ContextSettings.MAX_LOCAL_HISTORY_SIZE.getValue();
    while (localSizeHistory.size() > maxLocalHistorySize && !localSizeHistory.isEmpty()) {
        localSizeHistory.remove(0);
    }

    if (!localSizeHistory.isEmpty()) {
        BigInteger localSizeSum = BigInteger.ZERO;
        for (BigInteger size : localSizeHistory) {
            localSizeSum = localSizeSum.add(size);
        }

        localSize = localSizeSum.divide(BigInteger.valueOf(localSizeHistory.size()));
    }

    // Get the combined average
    // S = (localEstimation + sum(remoteEstimation[i]))/count
    BigInteger combinedSize = localSize;
    if (ContextSettings.COUNT_REMOTE_SIZE.getValue()) {
        // Prune all duplicates and sort the values
        Set<BigInteger> remoteSizeSet = new TreeSet<BigInteger>(remoteSizeHistory);

        if (remoteSizeSet.size() >= 3) {
            BigInteger[] remote = remoteSizeSet.toArray(new BigInteger[0]);

            // Skip the smallest and largest values
            int count = 1;
            int skip = ContextSettings.SKIP_REMOTE_ESTIMATES.getValue();
            for (int i = skip; (skip >= 0) && (i < (remote.length - skip)); i++) {
                combinedSize = combinedSize.add(remote[i]);
                count++;
            }
            combinedSize = combinedSize.divide(BigInteger.valueOf(count));

            // Make sure we didn't exceed the MAXIMUM number as
            // we made an addition with the local estimation which
            // might be already 2**160 bit!
            combinedSize = combinedSize.min(MAXIMUM);
        }
    }

    // There is always us!
    return BigInteger.ONE.max(combinedSize);
}