/** * Create a "magic number" for use in hashCode calculations. * @param seed byte[] to seed with. * @return a magic number as int. */ protected static int getMagicNumber(byte[] seed) { return new BigInteger(seed).intValue(); }
checkNonNegative("x", x); if (fitsInLong(x)) { return BigInteger.valueOf(LongMath.sqrt(x.longValue(), mode)); case CEILING: case UP: int sqrtFloorInt = sqrtFloor.intValue(); boolean sqrtFloorIsExact = (sqrtFloorInt * sqrtFloorInt == x.intValue()) // fast check mod 2^32 && sqrtFloor.pow(2).equals(x); // slow exact check 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();
final BigInteger uvp = BigInteger.valueOf(numerator).multiply(BigInteger.valueOf(fraction.denominator / d1)); final BigInteger upv = BigInteger.valueOf(fraction.numerator).multiply(BigInteger.valueOf(denominator / d1)); final BigInteger t = isAdd ? uvp.add(upv) : uvp.subtract(upv); final int tmodd1 = t.mod(BigInteger.valueOf(d1)).intValue(); final int d2 = tmodd1 == 0 ? d1 : greatestCommonDivisor(tmodd1, d1); throw new ArithmeticException("overflow: numerator too large after multiply"); return new Fraction(w.intValue(), mulPosAndCheck(denominator / d1, fraction.denominator / d2));
/** * Get exactly number value and type. * * @param value string to be converted * @param radix radix * @return exactly number value and type */ public static Number getExactlyNumber(final String value, final int radix) { BigInteger result = new BigInteger(value, radix); if (result.compareTo(new BigInteger(String.valueOf(Integer.MIN_VALUE))) >= 0 && result.compareTo(new BigInteger(String.valueOf(Integer.MAX_VALUE))) <= 0) { return result.intValue(); } if (result.compareTo(new BigInteger(String.valueOf(Long.MIN_VALUE))) >= 0 && result.compareTo(new BigInteger(String.valueOf(Long.MAX_VALUE))) <= 0) { return result.longValue(); } return result; } }
private static String calculateIbanChecksum(String countryCode, String basicBankAccountNumber) { String basis = basicBankAccountNumber + countryCode + "00"; StringBuilder sb = new StringBuilder(); char[] characters = basis.toLowerCase().toCharArray(); for (char c : characters) { if (Character.isLetter(c)) { sb.append(String.valueOf((c - 'a') + 10)); } else { sb.append(c); } } int mod97 = new BigInteger(sb.toString()).mod(BigInteger.valueOf(97L)).intValue(); return StringUtils.leftPad(String.valueOf(98 - mod97), 2, '0'); }
/** * Returns encrypted integer * * @param data_chunk * * @return */ private int getEncint(byte[] data_chunk) { byte ob; BigInteger bi = BigInteger.ZERO; byte[] nb = new byte[1]; if (placeHolder < data_chunk.length) { while ((ob = data_chunk[placeHolder]) < 0) { nb[0] = (byte) ((ob & 0x7f)); bi = bi.shiftLeft(7).add(new BigInteger(nb)); setPlaceHolder(placeHolder + 1); } nb[0] = (byte) ((ob & 0x7f)); bi = bi.shiftLeft(7).add(new BigInteger(nb)); setPlaceHolder(placeHolder + 1); } return bi.intValue(); }
@SuppressWarnings("unchecked") public static final <ARRAY> ARRAY bitOxr(final ARRAY b1 , final ARRAY b2 , BigInteger size , ByteWrapper<ARRAY> byteWrapper) { final BigInteger answers[] = size.divideAndRemainder(ByteOperation.EIGHT); ARRAY result = (ARRAY) Array.newInstance(b1.getClass().getComponentType() , answers[1].intValue() == 0 ? answers[0].intValue() : answers[0].intValue() + 1); ByteOperation.fill(0 , BigInteger.ZERO , size , result , byteWrapper); for (BigInteger i = BigInteger.ZERO ; i.compareTo(size) == -1 ; i = i.add(BigInteger.ONE)) if (ByteOperation.bitGet(i , b1 , byteWrapper) != ByteOperation.bitGet(i , b2 , byteWrapper)) ByteOperation.bitSet(i , 1 , result , byteWrapper); else ByteOperation.bitSet(i , 0 , result , byteWrapper); return result; }
checkNonNegative("x", x); if (fitsInLong(x)) { return BigInteger.valueOf(LongMath.sqrt(x.longValue(), mode)); case CEILING: case UP: int sqrtFloorInt = sqrtFloor.intValue(); boolean sqrtFloorIsExact = (sqrtFloorInt * sqrtFloorInt == x.intValue()) // fast check mod 2^32 && sqrtFloor.pow(2).equals(x); // slow exact check 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();
/** * Converts decoded ASN.1 Integer to int value. * If the object represents an integer value * larger than 32 bits, the high bits will be lost. * * @param decoded a decoded object corresponding to this type * @return decoded int value. */ public static int toIntValue(Object decoded) { return new BigInteger((byte[]) decoded).intValue(); }
BigInteger uvp = BigInteger.valueOf(numerator) .multiply(BigInteger.valueOf(fraction.denominator/d1)); BigInteger upv = BigInteger.valueOf(fraction.numerator) .multiply(BigInteger.valueOf(denominator/d1)); BigInteger t = isAdd ? uvp.add(upv) : uvp.subtract(upv); int tmodd1 = t.mod(BigInteger.valueOf(d1)).intValue(); int d2 = (tmodd1==0)?d1:greatestCommonDivisor(tmodd1, d1); (w.intValue(), mulPosAndCheck(denominator/d1, fraction.denominator/d2));
/** * Power of a BigInteger to a BigInteger certain exponent. Called by the '**' operator. * * @param self a BigInteger * @param exponent a BigInteger exponent * @return a BigInteger to the power of a the exponent * @since 2.3.8 */ public static BigInteger power(BigInteger self, BigInteger exponent) { if ((exponent.signum() >= 0) && (exponent.compareTo(BI_INT_MAX) <= 0)) { return self.pow(exponent.intValue()); } else { return BigDecimal.valueOf(Math.pow(self.doubleValue(), exponent.doubleValue())).toBigInteger(); } }
public static void main(String... args) { BigInteger fact = fact(100); System.out.println("fact(100) = " + fact); System.out.println("fact(100).longValue() = " + fact.longValue()); System.out.println("fact(100).intValue() = " + fact.intValue()); int powerOfTwoCount = 0; BigInteger two = BigInteger.valueOf(2); while (fact.compareTo(BigInteger.ZERO) > 0 && fact.mod(two).equals(BigInteger.ZERO)) { powerOfTwoCount++; fact = fact.divide(two); } System.out.println("fact(100) powers of two = " + powerOfTwoCount); } private static BigInteger fact(long n) { BigInteger result = BigInteger.ONE; for (long i = 2; i <= n; i++) result = result.multiply(BigInteger.valueOf(i)); return result; }
BigInteger total = new Biginteger("1000000000000000"); BigInteger[] series = new BigInteger[total.intValue()]; series[0]=BigInteger.ZERO; series[1]=BigInteger.ONE; series[i] = series[i-1].add(series[i-2])`