public static BigInteger castToBigInteger(Object val) { if (val == null) { return null; } if (val instanceof BigInteger) { return (BigInteger) val; } if (val instanceof String) { return new BigInteger((String) val); } return BigInteger.valueOf(((Number) val).longValue()); }
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"
@Override public byte[][] split(int n) { Preconditions.checkArgument(lastRowInt.compareTo(firstRowInt) > 0, "last row (%s) is configured less than first row (%s)", lastRow, firstRow); // +1 to range because the last row is inclusive BigInteger range = lastRowInt.subtract(firstRowInt).add(BigInteger.ONE); Preconditions.checkState(range.compareTo(BigInteger.valueOf(n)) >= 0, "split granularity (%s) is greater than the range (%s)", n, range); BigInteger[] splits = new BigInteger[n - 1]; BigInteger sizeOfEachSplit = range.divide(BigInteger.valueOf(n)); for (int i = 1; i < n; i++) { // NOTE: this means the last region gets all the slop. // This is not a big deal if we're assuming n << MAXHEX splits[i - 1] = firstRowInt.add(sizeOfEachSplit.multiply(BigInteger .valueOf(i))); } return convertToBytes(splits); }
BigInteger i = BigInteger.valueOf((long) recId / 2); BigInteger x = sig.r.add(i.multiply(n)); if (x.compareTo(prime) >= 0) { BigInteger e = new BigInteger(1, message); 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); return new BigInteger(1, Arrays.copyOfRange(qBytes, 1, qBytes.length));
checkNonNegative("x", x); if (fitsInLong(x)) { return BigInteger.valueOf(LongMath.sqrt(x.longValue(), mode)); return sqrtFloorIsExact ? sqrtFloor : sqrtFloor.add(BigInteger.ONE); case HALF_DOWN: case HALF_UP: case HALF_EVEN: BigInteger halfSquare = sqrtFloor.pow(2).add(sqrtFloor); return (halfSquare.compareTo(x) >= 0) ? sqrtFloor : sqrtFloor.add(BigInteger.ONE); default: throw new AssertionError();
private static long bigIntegerDecimalToGenericIntegerType(BigInteger bigInteger, int sourceScale, long minValue, long maxValue) { BigDecimal bigDecimal = new BigDecimal(bigInteger, sourceScale); BigInteger unscaledValue = bigDecimal.setScale(0, FLOOR).unscaledValue(); if (unscaledValue.compareTo(BigInteger.valueOf(maxValue)) > 0) { return maxValue; } if (unscaledValue.compareTo(BigInteger.valueOf(minValue)) < 0) { return minValue; } return unscaledValue.longValueExact(); }
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()); }
@Override public ValidationResult validate(BlockHeader header) { if (new BigInteger(1, header.getGasLimit()).compareTo(BigInteger.valueOf(MIN_GAS_LIMIT)) < 0) { return fault("header.getGasLimit() < MIN_GAS_LIMIT"); } return Success; } }
@Override public BigInteger getTokenCountInRange(String startToken, String endToken) { long start = Long.parseLong(startToken); long end = Long.parseLong(endToken); if (start == end) { if (start == MIN_TOKEN) { return TOTAL_TOKEN_COUNT; } else { return ZERO; } } BigInteger result = BigInteger.valueOf(end).subtract(BigInteger.valueOf(start)); if (end <= start) { result = result.add(TOTAL_TOKEN_COUNT); } return result; } }
@Test public void testGetRingFraction() { assertEquals(tokenRing.getTokenCountInRange("0", "1"), ONE); assertEquals(tokenRing.getTokenCountInRange("0", "200"), new BigInteger("200")); assertEquals(tokenRing.getTokenCountInRange("0", "10"), new BigInteger("10")); assertEquals(tokenRing.getTokenCountInRange("1", "11"), new BigInteger("10")); assertEquals(tokenRing.getTokenCountInRange("0", "0"), ZERO); assertEquals(tokenRing.getTokenCountInRange("-1", "-1"), BigInteger.valueOf(2).pow(127).add(ONE)); assertEquals(tokenRing.getTokenCountInRange("1", "0"), BigInteger.valueOf(2).pow(127)); }
@Test public void testGetTokenCountInRange() { assertEquals(tokenRing.getTokenCountInRange("0", "1"), ONE); assertEquals(tokenRing.getTokenCountInRange("-1", "1"), new BigInteger("2")); assertEquals(tokenRing.getTokenCountInRange("-100", "100"), new BigInteger("200")); assertEquals(tokenRing.getTokenCountInRange("0", "10"), new BigInteger("10")); assertEquals(tokenRing.getTokenCountInRange("1", "11"), new BigInteger("10")); assertEquals(tokenRing.getTokenCountInRange("0", "0"), ZERO); assertEquals(tokenRing.getTokenCountInRange("1", "1"), ZERO); assertEquals(tokenRing.getTokenCountInRange(Long.toString(Long.MIN_VALUE), Long.toString(Long.MIN_VALUE)), BigInteger.valueOf(2).pow(64).subtract(ONE)); assertEquals(tokenRing.getTokenCountInRange("1", "0"), BigInteger.valueOf(2).pow(64).subtract(BigInteger.valueOf(2))); }
/** * binomial (n choose m). * * @param n the numerator. Equals the size of the set to choose from. * @param m the denominator. Equals the number of elements to select. * @return the binomial coefficient. */ public static Rational binomial(Rational n, BigInteger m) { if (m.compareTo(BigInteger.ZERO) == 0) { return Rational.ONE; } Rational bin = n; for (BigInteger i = BigInteger.valueOf(2); i.compareTo(m) != 1; i = i.add(BigInteger.ONE)) { bin = bin.multiply(n.subtract(i.subtract(BigInteger.ONE))).divide(i); } return bin; } /* Rational.binomial */
@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(); }
compRem = remainder.abs().shiftLeftOneBit().compareTo(scaledDivisor.abs()); compRem = roundingBehavior(quotient.testBit(0) ? 1 : 0, sign * (5 + compRem), roundingMode); return valueOf(quotient.longValue() + compRem,scale); quotient = quotient.add(BigInteger.valueOf(compRem)); return new BigDecimal(quotient, scale);
public long mine(long fullSize, int[] dataset, byte[] blockHeaderTruncHash, long difficulty, long startNonce) { long nonce = startNonce; BigInteger target = valueOf(2).pow(256).divide(valueOf(difficulty)); while (!Thread.currentThread().isInterrupted()) { nonce++; Pair<byte[], byte[]> pair = hashimotoFull(fullSize, dataset, blockHeaderTruncHash, longToBytes(nonce)); BigInteger h = new BigInteger(1, pair.getRight() /* ?? */); if (h.compareTo(target) < 0) break; } return nonce; }