@GwtCompatible(emulated=true) public final class BigIntegerMath extends Object
BigInteger.
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 int and for long can be found in IntMath and LongMath respectively.
| Modifier and Type | Method and Description |
|---|---|
static BigInteger |
binomial(int n, int k)
Returns
n choose
k, also known as the binomial coefficient of
n and
k, that is,
n! / (k! (n - k)!).
|
static BigInteger |
divide(BigInteger
Returns the result of dividing
p by
q, rounding using the specified
RoundingMode.
|
static BigInteger |
factorial(int n)
Returns
n!, that is, the product of the first
n positive integers, or
1 if
n == 0.
|
static boolean |
isPowerOfTwo(BigInteger
Returns
true if
x represents a power of two.
|
static int |
log10(BigInteger
Returns the base-10 logarithm of
x, rounded according to the specified rounding mode.
|
static int |
log2(BigInteger
Returns the base-2 logarithm of
x, rounded according to the specified rounding mode.
|
static BigInteger |
sqrt(BigInteger
Returns the square root of
x, rounded with the specified rounding mode.
|
public static boolean isPowerOfTwo(BigIntegerx)
true if
x represents a power of two.
public static int log2(BigIntegerx, RoundingMode mode)
x, rounded according to the specified rounding mode.
IllegalArgumentException - if
x <= 0
ArithmeticException - if
mode is
RoundingMode.UNNECESSARY and
x is not a power of two
@GwtIncompatible(value="TODO") public static int log10(BigIntegerx, RoundingMode mode)
x, rounded according to the specified rounding mode.
IllegalArgumentException - if
x <= 0
ArithmeticException - if
mode is
RoundingMode.UNNECESSARY and
x is not a power of ten
@GwtIncompatible(value="TODO") public static BigIntegersqrt(BigInteger x, RoundingMode mode)
x, rounded with the specified rounding mode.
IllegalArgumentException - if
x < 0
ArithmeticException - if
mode is
RoundingMode.UNNECESSARY and
sqrt(x) is not an integer
@GwtIncompatible(value="TODO") public static BigIntegerdivide(BigInteger p, BigInteger q, RoundingMode mode)
p by
q, rounding using the specified
RoundingMode.
ArithmeticException - if
q == 0, or if
mode == UNNECESSARY and
a is not an integer multiple of
b
public static BigIntegerfactorial(int n)
n!, that is, the product of the first
n positive integers, or
1 if
n == 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).
IllegalArgumentException - if
n < 0
public static BigIntegerbinomial(int n, int k)
n choose
k, also known as the binomial coefficient of
n and
k, that is,
n! / (k! (n - k)!).
Warning: the result can take as much as O(k log n) space.
IllegalArgumentException - if
n < 0,
k < 0, or
k > n