Package com.google.common.math
Class BigIntegerMath
- java.lang.Object
-
- com.google.common.math.BigIntegerMath
-
@GwtCompatible(emulated=true) public final class BigIntegerMath extends Object
A class for arithmetic on values of typeBigInteger.The implementations of many methods in this class are based on material from Henry S. Warren, Jr.'s Hacker's Delight, (Addison Wesley, 2002).
Similar functionality for
intand forlongcan be found inIntMathandLongMathrespectively.- Since:
- 11.0
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static BigIntegerbinomial(int n, int k)Returnsnchoosek, also known as the binomial coefficient ofnandk, that is,n! / (k! (n - k)!).static BigIntegerdivide(BigInteger p, BigInteger q, RoundingMode mode)Returns the result of dividingpbyq, rounding using the specifiedRoundingMode.static BigIntegerfactorial(int n)Returnsn!, that is, the product of the firstnpositive integers, or1ifn == 0.static booleanisPowerOfTwo(BigInteger x)Returnstrueifxrepresents a power of two.static intlog10(BigInteger x, RoundingMode mode)Returns the base-10 logarithm ofx, rounded according to the specified rounding mode.static intlog2(BigInteger x, RoundingMode mode)Returns the base-2 logarithm ofx, rounded according to the specified rounding mode.static BigIntegersqrt(BigInteger x, RoundingMode mode)Returns the square root ofx, rounded with the specified rounding mode.
-
-
-
Method Detail
-
isPowerOfTwo
public static boolean isPowerOfTwo(BigInteger x)
Returnstrueifxrepresents a power of two.
-
log2
public static int log2(BigInteger x, RoundingMode mode)
Returns the base-2 logarithm ofx, rounded according to the specified rounding mode.- Throws:
IllegalArgumentException- ifx <= 0ArithmeticException- ifmodeisRoundingMode.UNNECESSARYandxis not a power of two
-
log10
@GwtIncompatible("TODO") public static int log10(BigInteger x, RoundingMode mode)
Returns the base-10 logarithm ofx, rounded according to the specified rounding mode.- Throws:
IllegalArgumentException- ifx <= 0ArithmeticException- ifmodeisRoundingMode.UNNECESSARYandxis not a power of ten
-
sqrt
@GwtIncompatible("TODO") public static BigInteger sqrt(BigInteger x, RoundingMode mode)
Returns the square root ofx, rounded with the specified rounding mode.- Throws:
IllegalArgumentException- ifx < 0ArithmeticException- ifmodeisRoundingMode.UNNECESSARYandsqrt(x)is not an integer
-
divide
@GwtIncompatible("TODO") public static BigInteger divide(BigInteger p, BigInteger q, RoundingMode mode)
Returns the result of dividingpbyq, rounding using the specifiedRoundingMode.- Throws:
ArithmeticException- ifq == 0, or ifmode == UNNECESSARYandais not an integer multiple ofb
-
factorial
public static BigInteger factorial(int n)
Returnsn!, that is, the product of the firstnpositive integers, or1ifn == 0.Warning: the result takes O(n log n) space, so use cautiously.
This uses an efficient binary recursive algorithm to compute the factorial with balanced multiplies. It also removes all the 2s from the intermediate products (shifting them back in at the end).
- Throws:
IllegalArgumentException- ifn < 0
-
binomial
public static BigInteger binomial(int n, int k)
Returnsnchoosek, also known as the binomial coefficient ofnandk, that is,n! / (k! (n - k)!).Warning: the result can take as much as O(k log n) space.
- Throws:
IllegalArgumentException- ifn < 0,k < 0, ork > n
-
-