List of usage examples for java.math BigInteger divide
public BigInteger divide(BigInteger val)
From source file:Main.java
static BigInteger inverseMod(BigInteger a, BigInteger b) { BigInteger b0 = b, t, q;//from w w w. j ava 2 s. co m 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:cc.redberry.core.number.NumberUtils.java
/** * Computes the integer square root of a number. * * @param n The number./*from w w w . j a va2s . c o m*/ * @return The integer square root, i.e. the largest number whose square * doesn't exceed n. */ public static BigInteger sqrt(BigInteger n) { if (n.signum() >= 0) { final int bitLength = n.bitLength(); BigInteger root = BigInteger.ONE.shiftLeft(bitLength / 2); while (!isSqrtXXX(n, root)) root = root.add(n.divide(root)).divide(TWO); return root; } else throw new ArithmeticException("square root of negative number"); }
From source file:gedi.util.math.stat.distributions.OccupancyNumberDistribution.java
private static BigInteger binomialCoefficientLargeInteger(final int n, final int k) { if ((n == k) || (k == 0)) { return BigInteger.valueOf(1); }/*from ww w. j a va2 s. co m*/ if ((k == 1) || (k == n - 1)) { return BigInteger.valueOf(n); } if (k > n / 2) { return binomialCoefficientLargeInteger(n, n - k); } BigInteger result = BigInteger.valueOf(1); int i = n - k + 1; for (int j = 1; j <= k; j++) { final int d = gcd(i, j); result = result.divide(BigInteger.valueOf(j / d)).multiply(BigInteger.valueOf(i / d)); i++; } return result; }
From source file:cc.redberry.core.number.Exponentiation.java
public static Complex findIntegerRoot(Complex base, BigInteger power) { BigInteger rDenominator = ((Rational) base.getReal()).getDenominator(); BigInteger iDenominator = ((Rational) base.getImaginary()).getDenominator(); BigInteger lcm = rDenominator.gcd(iDenominator); lcm = rDenominator.divide(lcm); lcm = lcm.multiply(iDenominator);//from w w w. j av a 2s . c o m BigInteger lcmRoot = findIntegerRoot(lcm, power); if (lcm == null) return null; base = base.multiply(lcm); Complex numericValue = base.pow(1.0 / power.doubleValue()); double real = numericValue.getReal().doubleValue(); double imaginary = numericValue.getImaginary().doubleValue(); int ceilReal = (int) Math.ceil(real), floorReal = (int) Math.floor(real), ceilImaginary = (int) Math.ceil(imaginary), floorImaginary = (int) Math.floor(imaginary); Complex candidate; if ((candidate = new Complex(ceilReal, ceilImaginary)).pow(power).equals(base)) return candidate.divide(lcmRoot); if ((candidate = new Complex(floorReal, ceilImaginary)).pow(power).equals(base)) return candidate.divide(lcmRoot); if ((candidate = new Complex(ceilReal, floorImaginary)).pow(power).equals(base)) return candidate.divide(lcmRoot); if ((candidate = new Complex(floorReal, floorImaginary)).pow(power).equals(base)) return candidate.divide(lcmRoot); return null; }
From source file:Main.java
/** * Compute the square root of x to a given scale, x >= 0. Use Newton's * algorithm.//from w ww.j a v a2s. c o m * * @param x * the value of x * @return the result value */ public static BigDecimal sqrt(BigDecimal x) { // Check that x >= 0. if (x.signum() < 0) { throw new ArithmeticException("x < 0"); } // n = x*(10^(2*SCALE)) BigInteger n = x.movePointRight(SCALE << 1).toBigInteger(); // The first approximation is the upper half of n. int bits = (n.bitLength() + 1) >> 1; BigInteger ix = n.shiftRight(bits); BigInteger ixPrev; // Loop until the approximations converge // (two successive approximations are equal after rounding). do { ixPrev = ix; // x = (x + n/x)/2 ix = ix.add(n.divide(ix)).shiftRight(1); Thread.yield(); } while (ix.compareTo(ixPrev) != 0); return new BigDecimal(ix, SCALE); }
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. java 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:piuk.blockchain.android.util.WalletUtils.java
public static String formatValue(final BigInteger value, final String plusSign, final String minusSign) { final boolean negative = value.compareTo(BigInteger.ZERO) < 0; final BigInteger absValue = value.abs(); final String sign = negative ? minusSign : plusSign; final int coins = absValue.divide(Utils.COIN).intValue(); final int cents = absValue.remainder(Utils.COIN).intValue(); if (cents % 1000000 == 0) return String.format("%s%d.%02d", sign, coins, cents / 1000000); else if (cents % 10000 == 0) return String.format("%s%d.%04d", sign, coins, cents / 10000); else// w w w.j a v a2 s . c o m return String.format("%s%d.%08d", sign, coins, cents); }
From source file:com.docd.purefm.utils.PFMFileUtils.java
@NonNull public static String byteCountToDisplaySize(@NonNull final BigInteger size) { String displaySize;// ww w. j ava 2 s .c om if (size.divide(FileUtils.ONE_GB_BI).compareTo(BigInteger.ZERO) > 0) { displaySize = String.valueOf(size.divide(FileUtils.ONE_GB_BI)) + " GiB"; } else if (size.divide(FileUtils.ONE_MB_BI).compareTo(BigInteger.ZERO) > 0) { displaySize = String.valueOf(size.divide(FileUtils.ONE_MB_BI)) + " MiB"; } else if (size.divide(FileUtils.ONE_KB_BI).compareTo(BigInteger.ZERO) > 0) { displaySize = String.valueOf(size.divide(FileUtils.ONE_KB_BI)) + " KiB"; } else { displaySize = String.valueOf(size) + " B"; } return displaySize; }
From source file:Bytes.java
/** * Convert the specified amount into a human readable (though slightly less accurate) * result. IE://from w w w .j a v a2 s. c om * '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:Util.java
protected static BigInteger sqrt(BigInteger number, BigInteger guess) { // ((n/g) + g)/2: until same result twice in a row // BigInteger result = number.divide(guess).add(guess).divide(BigIntegerTWO); // if(result.compareTo(guess) == 0) // return result; ///* w ww . j a v a 2 s . c om*/ // return sqrt(number, result); // redoing this to avoid StackOverFlow BigInteger result = BigIntegerZERO; BigInteger flipA = result; BigInteger flipB = result; boolean first = true; while (result.compareTo(guess) != 0) { if (!first) guess = result; else first = false; result = number.divide(guess).add(guess).divide(BigIntegerTWO); // handle flip flops if (result.equals(flipB)) return flipA; flipB = flipA; flipA = result; } return result; }