/** * Compares the value of this with another constant. * * @param val the other constant to compare with * @return -1, 0 or 1 if this number is numerically less than, equal to, * or greater than val. */ public int compareTo(final Rational val) { /* Since we have always kept the denominators positive, * simple cross-multiplying works without changing the sign. */ final BigInteger left = a.multiply(val.b); final BigInteger right = val.a.multiply(b); return left.compareTo(right); } /* Rational.compareTo */
static BigInteger binomial(final int N, final int K) { BigInteger ret = BigInteger.ONE; for (int k = 0; k < K; k++) { ret = ret.multiply(BigInteger.valueOf(N-k)) .divide(BigInteger.valueOf(k+1)); } return ret; } //... System.out.println(binomial(133, 71)); // prints "555687036928510235891585199545206017600"
boolean first = true; while (true) { if (FOUR.multiply(q).add(r).subtract(t).compareTo(n.multiply(t)) == -1) { System.out.print(n); if (first) { first = false; nr = BigInteger.TEN.multiply(r.subtract(n.multiply(t))); n = BigInteger.TEN.multiply(THREE.multiply(q).add(r)).divide(t) .subtract(BigInteger.TEN.multiply(n)); q = q.multiply(BigInteger.TEN); r = nr; System.out.flush(); } else { nr = TWO.multiply(q).add(r).multiply(l); nn = q.multiply(SEVEN.multiply(k)).add(TWO).add(r.multiply(l)) .divide(t.multiply(l)); q = q.multiply(k); t = t.multiply(l); l = l.add(TWO); k = k.add(BigInteger.ONE);
public static BigInteger fastBigIntegerValueUnscaled( int fastSignum, long fast0, long fast1, long fast2) { if (fastSignum == 0) { return BigInteger.ZERO; } BigInteger result; if (fast2 == 0) { if (fast1 == 0) { result = BigInteger.valueOf(fast0); } else { result = BigInteger.valueOf(fast0).add( BigInteger.valueOf(fast1).multiply(BIG_INTEGER_LONGWORD_MULTIPLIER)); } } else { result = BigInteger.valueOf(fast0).add( BigInteger.valueOf(fast1).multiply(BIG_INTEGER_LONGWORD_MULTIPLIER)).add( BigInteger.valueOf(fast2).multiply(BIG_INTEGER_LONGWORD_MULTIPLIER_2X)); } return (fastSignum == 1 ? result : result.negate()); }
BigInteger i = BigInteger.valueOf((long) recId / 2); BigInteger x = sig.r.add(i.multiply(n)); if (x.compareTo(prime) >= 0) { BigInteger eInv = BigInteger.ZERO.subtract(e).mod(n); BigInteger rInv = sig.r.modInverse(n); BigInteger srInv = rInv.multiply(sig.s).mod(n); BigInteger eInvrInv = rInv.multiply(eInv).mod(n); ECPoint q = ECAlgorithms.sumOfTwoMultiplies(CURVE.getG(), eInvrInv, R, srInv);
private static ECPoint doublePoint(ECPoint r, EllipticCurve curve) { if (r.equals(ECPoint.POINT_INFINITY)) return r; BigInteger slope = (r.getAffineX().pow(2)).multiply(THREE); slope = slope.add(curve.getA()); BigInteger prime = ((ECFieldFp) curve.getField()).getP(); // use NBI modInverse(); BigInteger tmp = r.getAffineY().multiply(TWO); tmp = new NativeBigInteger(tmp); slope = slope.multiply(tmp.modInverse(prime)); BigInteger xOut = slope.pow(2).subtract(r.getAffineX().multiply(TWO)).mod(prime); BigInteger yOut = (r.getAffineY().negate()).add(slope.multiply(r.getAffineX().subtract(xOut))).mod(prime); ECPoint out = new ECPoint(xOut, yOut); return out; }
@Override protected long vertexCount() { BigInteger vertexCount = BigInteger.ONE; for (Dimension dimension : dimensions) { vertexCount = vertexCount.multiply(BigInteger.valueOf(dimension.size)); } if (vertexCount.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0) { throw new ProgramParametrizationException("Number of vertices in grid graph '" + vertexCount + "' is greater than Long.MAX_VALUE."); } return vertexCount.longValue(); }
int approxCmp = approxPow.compareTo(x); approxPow = approxPow.divide(BigInteger.TEN); approxCmp = approxPow.compareTo(x); } while (approxCmp > 0); } else { BigInteger nextPow = BigInteger.TEN.multiply(approxPow); int nextCmp = nextPow.compareTo(x); while (nextCmp <= 0) { approxLog10++; approxPow = nextPow; approxCmp = nextCmp; nextPow = BigInteger.TEN.multiply(approxPow); nextCmp = nextPow.compareTo(x); BigInteger halfPowerSquared = floorPow.pow(2).multiply(BigInteger.TEN); return (x2.compareTo(halfPowerSquared) <= 0) ? floorLog : floorLog + 1; default:
@Override public BigInteger calcDifficulty(BlockHeader curBlock, BlockHeader parent) { BigInteger pd = parent.getDifficultyBI(); BigInteger quotient = pd.divide(getConstants().getDIFFICULTY_BOUND_DIVISOR()); BigInteger sign = getCalcDifficultyMultiplier(curBlock, parent); BigInteger fromParent = pd.add(quotient.multiply(sign)); BigInteger difficulty = max(getConstants().getMINIMUM_DIFFICULTY(), fromParent); int explosion = getExplosion(curBlock, parent); if (explosion >= 0) { difficulty = max(getConstants().getMINIMUM_DIFFICULTY(), difficulty.add(BigInteger.ONE.shiftLeft(explosion))); } return difficulty; }
@Test public void testShiftRightArray8() { assertShiftRightArray8(TWO.pow(1), 0); assertShiftRightArray8(TWO.pow(1), 1); assertShiftRightArray8(TWO.pow(1), 10); assertShiftRightArray8(TWO.pow(15).add(TWO.pow(3)), 2); assertShiftRightArray8(TWO.pow(15).add(TWO.pow(3)), 10); assertShiftRightArray8(TWO.pow(15).add(TWO.pow(3)), 20); assertShiftRightArray8(TWO.pow(70), 30); assertShiftRightArray8(TWO.pow(70).subtract(TWO.pow(1)), 30, true); assertShiftRightArray8(TWO.pow(70), 32); assertShiftRightArray8(TWO.pow(70).subtract(TWO.pow(1)), 32, true); assertShiftRightArray8(TWO.pow(120), 70); assertShiftRightArray8(TWO.pow(120).subtract(TWO.pow(1)), 70, true); assertShiftRightArray8(TWO.pow(120), 96); assertShiftRightArray8(TWO.pow(120).subtract(TWO.pow(1)), 96, true); assertShiftRightArray8(MAX_DECIMAL_UNSCALED_VALUE, 20, true); assertShiftRightArray8(MAX_DECIMAL_UNSCALED_VALUE.multiply(MAX_DECIMAL_UNSCALED_VALUE), 130); assertShiftRightArray8(TWO.pow(256).subtract(BigInteger.ONE), 130, true); assertShiftRightArray8Overflow(TWO.pow(156), 1); assertShiftRightArray8Overflow(MAX_DECIMAL_UNSCALED_VALUE.multiply(MAX_DECIMAL_UNSCALED_VALUE), 20); assertShiftRightArray8Overflow(TWO.pow(256).subtract(BigInteger.ONE), 129); }
BigInteger rescaledDividend = dividendBigInteger.multiply(bigIntegerTenToNth(dividendRescaleFactor)); BigInteger rescaledDivisor = divisorBigInteger.multiply(bigIntegerTenToNth(divisorRescaleFactor)); BigInteger[] expectedQuotientAndRemainder = rescaledDividend.divideAndRemainder(rescaledDivisor); BigInteger expectedQuotient = expectedQuotientAndRemainder[0]; BigInteger expectedRemainder = expectedQuotientAndRemainder[1]; boolean overflowIsExpected = expectedQuotient.abs().compareTo(bigIntegerTenToNth(38)) >= 0 || expectedRemainder.abs().compareTo(bigIntegerTenToNth(38)) >= 0;