/** * Compares this {@code BigInteger} with {@code value}. Returns {@code -1} * if {@code this < value}, {@code 0} if {@code this == value} and {@code 1} * if {@code this > value}, . * * @param value value to be compared with {@code this}. * @throws NullPointerException if {@code value == null}. */ public int compareTo(BigInteger value) { return BigInt.cmp(getBigInt(), value.getBigInt()); }
/** * Returns a {@code BigInteger} whose value is greatest common divisor * of {@code this} and {@code value}. If {@code this == 0} and {@code * value == 0} then zero is returned, otherwise the result is positive. * * @param value value with which the greatest common divisor is computed. * @throws NullPointerException if {@code value == null}. */ public BigInteger gcd(BigInteger value) { return new BigInteger(BigInt.gcd(getBigInt(), value.getBigInt())); }
/** * Returns the sign of this {@code BigInteger}. * * @return {@code -1} if {@code this < 0}, {@code 0} if {@code this == 0}, * {@code 1} if {@code this > 0}. */ public int signum() { if (javaIsValid) { return sign; } return getBigInt().sign(); }
/** * Returns a {@code BigInteger} whose value is {@code pow(this, exp)}. * * @throws ArithmeticException if {@code exp < 0}. */ public BigInteger pow(int exp) { if (exp < 0) { throw new ArithmeticException("exp < 0: " + exp); } return new BigInteger(BigInt.exp(getBigInt(), exp)); }
/** * Returns a string representation of this {@code BigInteger} in decimal * form. */ @Override public String toString() { return getBigInt().decString(); }
/** * Returns a {@code BigInteger} whose value is {@code 1/this mod m}. The * modulus {@code m} must be positive. The result is guaranteed to be in the * interval {@code [0, m)} (0 inclusive, m exclusive). If {@code this} is * not relatively prime to m, then an exception is thrown. * * @param m the modulus. * @throws NullPointerException if {@code m == null} * @throws ArithmeticException if {@code m < 0 or} if {@code this} is not * relatively prime to {@code m} */ public BigInteger modInverse(BigInteger m) { if (m.signum() <= 0) { throw new ArithmeticException("modulus not positive"); } return new BigInteger(BigInt.modInverse(getBigInt(), m.getBigInt())); }
/** * Returns a {@code BigInteger} whose value is {@code this mod m}. The * modulus {@code m} must be positive. The result is guaranteed to be in the * interval {@code [0, m)} (0 inclusive, m exclusive). The behavior of this * function is not equivalent to the behavior of the % operator defined for * the built-in {@code int}'s. * * @param m the modulus. * @return {@code this mod m}. * @throws NullPointerException if {@code m == null}. * @throws ArithmeticException if {@code m < 0}. */ public BigInteger mod(BigInteger m) { if (m.signum() <= 0) { throw new ArithmeticException("m.signum() <= 0"); } return new BigInteger(BigInt.modulus(getBigInt(), m.getBigInt())); }
/** * Returns a {@code BigInteger} whose value is {@code this / divisor}. * * @param divisor value by which {@code this} is divided. * @return {@code this / divisor}. * @throws NullPointerException if {@code divisor == null}. * @throws ArithmeticException if {@code divisor == 0}. */ public BigInteger divide(BigInteger divisor) { BigInt quotient = new BigInt(); BigInt.division(getBigInt(), divisor.getBigInt(), quotient, null); return new BigInteger(quotient); }
/** * Returns a {@code BigInteger} whose value is {@code this % divisor}. * Regarding signs this methods has the same behavior as the % operator on * ints: the sign of the remainder is the same as the sign of this. * * @param divisor value by which {@code this} is divided. * @throws NullPointerException if {@code divisor == null}. * @throws ArithmeticException if {@code divisor == 0}. */ public BigInteger remainder(BigInteger divisor) { BigInt remainder = new BigInt(); BigInt.division(getBigInt(), divisor.getBigInt(), null, remainder); return new BigInteger(remainder); }
/** * Tests whether this {@code BigInteger} is probably prime. If {@code true} * is returned, then this is prime with a probability greater than * {@code 1 - 1/2<sup>certainty</sup>)}. If {@code false} is returned, then this * is definitely composite. If the argument {@code certainty} <= 0, then * this method returns true. * * @param certainty tolerated primality uncertainty. * @return {@code true}, if {@code this} is probably prime, {@code false} * otherwise. */ public boolean isProbablePrime(int certainty) { if (certainty <= 0) { return true; } return getBigInt().isPrime(certainty); }
private static BigDecimal addAndMult10(BigDecimal thisValue,BigDecimal augend, int diffScale) { if(diffScale < MathUtils.LONG_POWERS_OF_TEN.length && Math.max(thisValue.bitLength,augend.bitLength+LONG_POWERS_OF_TEN_BIT_LENGTH[diffScale])+1<64) { return valueOf(thisValue.smallValue+augend.smallValue*MathUtils.LONG_POWERS_OF_TEN[diffScale],thisValue.scale); } else { BigInt bi = Multiplication.multiplyByTenPow(augend.getUnscaledValue(),diffScale).getBigInt(); bi.add(thisValue.getUnscaledValue().getBigInt()); return new BigDecimal(new BigInteger(bi), thisValue.scale); } }
/** * Returns a {@code BigInteger} whose value is {@code this + value}. */ public BigInteger add(BigInteger value) { BigInt lhs = getBigInt(); BigInt rhs = value.getBigInt(); if (rhs.sign() == 0) { return this; } if (lhs.sign() == 0) { return value; } return new BigInteger(BigInt.addition(lhs, rhs)); }
/** * Prepares this {@code BigInteger} for serialization, i.e. the * non-transient fields {@code signum} and {@code magnitude} are assigned. */ private void writeObject(ObjectOutputStream out) throws IOException { BigInt bigInt = getBigInt(); signum = bigInt.sign(); magnitude = bigInt.bigEndianMagnitude(); out.defaultWriteObject(); } }
/** * Multiplies a number by a positive integer. * @param val an arbitrary {@code BigInteger} * @param factor a positive {@code int} number * @return {@code val * factor} */ static BigInteger multiplyByPositiveInt(BigInteger val, int factor) { BigInt bi = val.getBigInt().copy(); bi.multiplyByPositiveInt(factor); return new BigInteger(bi); }
/** * Returns a two element {@code BigInteger} array containing * {@code this / divisor} at index 0 and {@code this % divisor} at index 1. * * @param divisor value by which {@code this} is divided. * @throws NullPointerException if {@code divisor == null}. * @throws ArithmeticException if {@code divisor == 0}. * @see #divide * @see #remainder */ public BigInteger[] divideAndRemainder(BigInteger divisor) { BigInt divisorBigInt = divisor.getBigInt(); BigInt quotient = new BigInt(); BigInt remainder = new BigInt(); BigInt.division(getBigInt(), divisorBigInt, quotient, remainder); return new BigInteger[] {new BigInteger(quotient), new BigInteger(remainder) }; }
/** * Returns a {@code BigInteger} whose value is the absolute value of {@code * this}. */ public BigInteger abs() { BigInt bigInt = getBigInt(); if (bigInt.sign() >= 0) { return this; } BigInt a = bigInt.copy(); a.setSign(1); return new BigInteger(a); }
/** * Returns a {@code BigInteger} whose value is the {@code -this}. */ public BigInteger negate() { BigInt bigInt = getBigInt(); int sign = bigInt.sign(); if (sign == 0) { return this; } BigInt a = bigInt.copy(); a.setSign(-sign); return new BigInteger(a); }
/** * Returns a string containing a string representation of this {@code * BigInteger} with base radix. If {@code radix < Character.MIN_RADIX} or * {@code radix > Character.MAX_RADIX} then a decimal representation is * returned. The characters of the string representation are generated with * method {@code Character.forDigit}. * * @param radix base to be used for the string representation. */ public String toString(int radix) { if (radix == 10) { return getBigInt().decString(); } else { prepareJavaRepresentation(); return Conversion.bigInteger2String(this, radix); } }