List of usage examples for java.math BigInteger multiply
BigInteger multiply(long v)
From source file:com.google.uzaygezen.core.ranges.BigIntegerRange.java
static BigInteger overlap(List<BigIntegerRange> x, List<BigIntegerRange> y) { int n = x.size(); Preconditions.checkArgument(y.size() == n, "x and y must have the same number of values"); BigInteger overlap = BigInteger.ONE; // Stop early if overlap.signum() becomes zero. for (int i = 0; i < n & overlap.signum() != 0; ++i) { BigIntegerRange xRange = x.get(i); BigIntegerRange yRange = y.get(i); overlap = overlap.multiply(xRange.overlap(yRange)); }//from w w w. j av a 2s. c o m return overlap; }
From source file:com.github.tell.arithmetic.integer.gmp.MPZBenchmarking.java
@SuppressWarnings("UnusedAssignment") private static StopWatch mulWithBigInteger(final int length) { final BigInteger x1 = new BigInteger(length, secureRandom); final BigInteger x2 = new BigInteger(length, secureRandom); @SuppressWarnings("unused") BigInteger x3 = BigInteger.valueOf(0); final StopWatch timer = new StopWatch(); timer.start();//from www . ja va2s. com for (int j = 0; j < numOfLoop; j++) { x3 = x1.multiply(x2); } timer.stop(); printout(timer, length, "BigInteger"); return timer; }
From source file:net.big_oh.common.utils.CollectionsUtil.java
protected static <T> BigInteger countCombinations(int n, int k) throws IllegalArgumentException { // sanity check if (k < 0) { throw new IllegalArgumentException("The value of the k parameter cannot be less than zero."); }//from w w w. j a va 2 s . c o m if (k > n) { throw new IllegalArgumentException( "The value of the k parameter cannot be greater than n, the size of the originalSet."); } // The end result will be equal to n! / (k! * ((n-k)!)) // start by doing some up front evaluation of the denominator int maxDenomArg; int minDenomArg; if ((n - k) > k) { maxDenomArg = (n - k); minDenomArg = k; } else { maxDenomArg = k; minDenomArg = (n - k); } // First, do an efficient calculation of n! / maxDenomArg! BigInteger partialCalculation = BigInteger.ONE; for (int i = maxDenomArg + 1; i <= n; i++) { partialCalculation = partialCalculation.multiply(BigInteger.valueOf(i)); } // Lastly, produce the final solution by calculating partialCalculation / minDenomArg! return partialCalculation.divide(MathUtil.factorial(minDenomArg)); }
From source file:Main.java
/** * Java runtime 1.5 is inconsistent with its handling of days in Duration objects. * @param duration A duration object to be normalised * @return A day-normalised duration, i.e. all years and months converted to days, * e.g. 1Y 3M 3D => 458 days/*from ww w . j ava 2 s . c om*/ */ private static javax.xml.datatype.Duration normaliseDays(javax.xml.datatype.Duration duration) { final long DAYS_PER_MONTH = 30; final long DAYS_PER_YEAR = 365; BigInteger days = (BigInteger) duration.getField(DatatypeConstants.DAYS); BigInteger months = (BigInteger) duration.getField(DatatypeConstants.MONTHS); BigInteger years = (BigInteger) duration.getField(DatatypeConstants.YEARS); BigInteger normalisedDays = years.multiply(BigInteger.valueOf(DAYS_PER_YEAR)); normalisedDays = normalisedDays.add(months.multiply(BigInteger.valueOf(DAYS_PER_MONTH))); normalisedDays = normalisedDays.add(days); BigInteger hours = (BigInteger) duration.getField(DatatypeConstants.HOURS); BigInteger minutes = (BigInteger) duration.getField(DatatypeConstants.MINUTES); BigDecimal seconds = (BigDecimal) duration.getField(DatatypeConstants.SECONDS); boolean positive = duration.getSign() >= 0; return FACTORY.newDuration(positive, BigInteger.ZERO, BigInteger.ZERO, normalisedDays, hours, minutes, seconds); }
From source file:Main.java
private static BigInteger findSquareRoot(BigInteger alpha, BigInteger p) { BigInteger beta = null;//from w w w . j av a2 s . com if (p.mod(BigInteger.valueOf(4)).compareTo(BigInteger.valueOf(3)) == 0) { BigInteger k = p.shiftRight(2).add(ONE); beta = alpha.modPow(k, p); } else if (p.mod(BigInteger.valueOf(8)).compareTo(BigInteger.valueOf(5)) == 0) { System.out.println("p = 8 mod 5"); BigInteger k = p.subtract(BigInteger.valueOf(5)).divide(BigInteger.valueOf(8)); BigInteger gamma = alpha.multiply(BigInteger.valueOf(2)).modPow(k, p); BigInteger i = alpha.multiply(BigInteger.valueOf(2)).multiply(gamma.pow(2)).mod(p); beta = alpha.multiply(gamma).multiply(i.subtract(ONE)).mod(p); } else if (p.mod(BigInteger.valueOf(8)).compareTo(BigInteger.valueOf(1)) == 0) { beta = null; //TODO System.out.println("finding square root not fully implemented yet"); } return beta; }
From source file:Bytes.java
/** * Convert the specified amount into a human readable (though slightly less accurate) * result. IE://from ww w. ja v a2 s . com * '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:CentralLimitTheorem.CLT.java
static BigInteger factorial(int n) { BigInteger res = BigInteger.ONE; for (int i = n; i > 1; i--) { res = res.multiply(BigInteger.valueOf(i)); }/*from ww w . ja v a 2s . c o m*/ return (res); }
From source file:edu.macalester.tagrelatedness.KendallsCorrelation.java
/** * Computes the Kendall's Tau rank correlation coefficient between the two * lists.//from w w w. j a v a 2 s . com * * @param xs first data list * @param ys second data list * @return Returns Kendall's Tau rank correlation coefficient for the two * lists * @throws DimensionMismatchException if the list lengths do not match */ static public <E extends Comparable<E>, F extends Comparable<F>> double correlation(final List<E> xs, final List<F> ys) { final int n = xs.size(); final long numPairs = (long) n * (n - 1) / 2; ComparablePair[] pairs = new ComparablePair[n]; for (int i = 0; i < n; i++) { pairs[i] = new ComparablePair(xs.get(i), ys.get(i)); } Arrays.sort(pairs); long tiedXPairs = 0; long tiedXYPairs = 0; long consecutiveXTies = 1; long consecutiveXYTies = 1; ComparablePair prev = pairs[0]; for (int i = 1; i < n; i++) { final ComparablePair curr = pairs[i]; if (curr.x.equals(prev.x)) { consecutiveXTies++; if (curr.y.equals(prev.y)) { consecutiveXYTies++; } else { tiedXYPairs += consecutiveXYTies * (consecutiveXYTies - 1) / 2; consecutiveXYTies = 1; } } else { tiedXPairs += consecutiveXTies * (consecutiveXTies - 1) / 2; consecutiveXTies = 1; tiedXYPairs += consecutiveXYTies * (consecutiveXYTies - 1) / 2; consecutiveXYTies = 1; } prev = curr; } tiedXPairs += consecutiveXTies * (consecutiveXTies - 1) / 2; tiedXYPairs += consecutiveXYTies * (consecutiveXYTies - 1) / 2; long swaps = 0; ComparablePair[] pairsDestination = new ComparablePair[n]; for (int segmentSize = 1; segmentSize < n; segmentSize <<= 1) { for (int offset = 0; offset < n; offset += 2 * segmentSize) { int i = offset; final int iEnd = Math.min(i + segmentSize, n); int j = iEnd; final int jEnd = Math.min(j + segmentSize, n); int copyLocation = offset; while (i < iEnd || j < jEnd) { if (i < iEnd) { if (j < jEnd) { if (pairs[i].y.compareTo(pairs[j].y) <= 0) { pairsDestination[copyLocation] = pairs[i]; i++; } else { pairsDestination[copyLocation] = pairs[j]; j++; swaps += iEnd - i; } } else { pairsDestination[copyLocation] = pairs[i]; i++; } } else { pairsDestination[copyLocation] = pairs[j]; j++; } copyLocation++; } } final ComparablePair[] pairsTemp = pairs; pairs = pairsDestination; pairsDestination = pairsTemp; } long tiedYPairs = 0; long consecutiveYTies = 1; prev = pairs[0]; for (int i = 1; i < n; i++) { final ComparablePair curr = pairs[i]; if (curr.y.equals(prev.y)) { consecutiveYTies++; } else { tiedYPairs += consecutiveYTies * (consecutiveYTies - 1) / 2; consecutiveYTies = 1; } prev = curr; } tiedYPairs += consecutiveYTies * (consecutiveYTies - 1) / 2; long concordantMinusDiscordant = (long) numPairs - tiedXPairs - tiedYPairs + tiedXYPairs - 2 * swaps; // The formula for tau is annotated below. Here, it is done with several objects to handle big numbers // concordantMinusDiscordant / Math.sqrt((numPairs - tiedXPairs) * (numPairs - tiedYPairs)); BigSquareRoot bigSqrt = new BigSquareRoot(); BigInteger numPairsMinusTiedX = new BigInteger("" + (numPairs - tiedXPairs)); BigInteger numPairsMinusTiedY = new BigInteger("" + (numPairs - tiedYPairs)); BigReal numerator = new BigReal(concordantMinusDiscordant); BigDecimal denominator = bigSqrt.get(numPairsMinusTiedX.multiply(numPairsMinusTiedY)); BigReal result = numerator.divide(new BigReal(denominator)); return result.doubleValue(); }
From source file:Main.java
public static byte[] decodeBase58(String input) { if (input == null) { return null; }/* www .j av a2 s. c o m*/ input = input.trim(); if (input.length() == 0) { return new byte[0]; } BigInteger resultNum = BigInteger.ZERO; int nLeadingZeros = 0; while (nLeadingZeros < input.length() && input.charAt(nLeadingZeros) == BASE58[0]) { nLeadingZeros++; } long acc = 0; int nDigits = 0; int p = nLeadingZeros; while (p < input.length()) { int v = BASE58_VALUES[input.charAt(p) & 0xff]; if (v >= 0) { acc *= 58; acc += v; nDigits++; if (nDigits == BASE58_CHUNK_DIGITS) { resultNum = resultNum.multiply(BASE58_CHUNK_MOD).add(BigInteger.valueOf(acc)); acc = 0; nDigits = 0; } p++; } else { break; } } if (nDigits > 0) { long mul = 58; while (--nDigits > 0) { mul *= 58; } resultNum = resultNum.multiply(BigInteger.valueOf(mul)).add(BigInteger.valueOf(acc)); } final int BASE58_SPACE = -2; while (p < input.length() && BASE58_VALUES[input.charAt(p) & 0xff] == BASE58_SPACE) { p++; } if (p < input.length()) { return null; } byte[] plainNumber = resultNum.toByteArray(); int plainNumbersOffs = plainNumber[0] == 0 ? 1 : 0; byte[] result = new byte[nLeadingZeros + plainNumber.length - plainNumbersOffs]; System.arraycopy(plainNumber, plainNumbersOffs, result, nLeadingZeros, plainNumber.length - plainNumbersOffs); return result; }
From source file:org.multibit.utils.CSMiscUtils.java
@Deprecated public static BigInteger calcRawFlatCharge(CSAsset asset) { CoinSparkGenesis genesis = asset.getGenesis(); short chargeExponent = genesis.getChargeFlatExponent(); short chargeMantissa = genesis.getChargeFlatMantissa(); if (0 == chargeMantissa) return BigInteger.ZERO; BigInteger n = new BigInteger("10").pow(chargeExponent); n = n.multiply(new BigInteger(String.valueOf(chargeMantissa))); return n;//from www. j a v a2 s .c o m }