@GwtCompatible(emulated=true) public final class BigIntegerMath extends java.lang.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 | Field and Description |
---|---|
private static double |
LN_10 |
private static double |
LN_2 |
(package private) static int |
SQRT2_PRECOMPUTE_THRESHOLD |
(package private) static java.math.BigInteger |
SQRT2_PRECOMPUTED_BITS |
Modifier | Constructor and Description |
---|---|
private |
BigIntegerMath() |
Modifier and Type | Method and Description |
---|---|
static java.math.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 java.math.BigInteger |
ceilingPowerOfTwo(java.math.BigInteger x)
Returns the smallest power of two greater than or equal to
x . |
static java.math.BigInteger |
divide(java.math.BigInteger p,
java.math.BigInteger q,
java.math.RoundingMode mode)
Returns the result of dividing
p by q , rounding using the specified
RoundingMode . |
static java.math.BigInteger |
factorial(int n)
Returns
n! , that is, the product of the first n positive integers, or 1
if n == 0 . |
(package private) static boolean |
fitsInLong(java.math.BigInteger x) |
static java.math.BigInteger |
floorPowerOfTwo(java.math.BigInteger x)
Returns the largest power of two less than or equal to
x . |
static boolean |
isPowerOfTwo(java.math.BigInteger x)
Returns
true if x represents a power of two. |
(package private) static java.math.BigInteger |
listProduct(java.util.List<java.math.BigInteger> nums) |
(package private) static java.math.BigInteger |
listProduct(java.util.List<java.math.BigInteger> nums,
int start,
int end) |
static int |
log10(java.math.BigInteger x,
java.math.RoundingMode mode)
Returns the base-10 logarithm of
x , rounded according to the specified rounding mode. |
static int |
log2(java.math.BigInteger x,
java.math.RoundingMode mode)
Returns the base-2 logarithm of
x , rounded according to the specified rounding mode. |
static java.math.BigInteger |
sqrt(java.math.BigInteger x,
java.math.RoundingMode mode)
Returns the square root of
x , rounded with the specified rounding mode. |
private static java.math.BigInteger |
sqrtApproxWithDoubles(java.math.BigInteger x) |
private static java.math.BigInteger |
sqrtFloor(java.math.BigInteger x) |
static final int SQRT2_PRECOMPUTE_THRESHOLD
static final java.math.BigInteger SQRT2_PRECOMPUTED_BITS
private static final double LN_10
private static final double LN_2
@Beta public static java.math.BigInteger ceilingPowerOfTwo(java.math.BigInteger x)
x
. This is equivalent to
BigInteger.valueOf(2).pow(log2(x, CEILING))
.java.lang.IllegalArgumentException
- if x <= 0
@Beta public static java.math.BigInteger floorPowerOfTwo(java.math.BigInteger x)
x
. This is equivalent to
BigInteger.valueOf(2).pow(log2(x, FLOOR))
.java.lang.IllegalArgumentException
- if x <= 0
public static boolean isPowerOfTwo(java.math.BigInteger x)
true
if x
represents a power of two.public static int log2(java.math.BigInteger x, java.math.RoundingMode mode)
x
, rounded according to the specified rounding mode.java.lang.IllegalArgumentException
- if x <= 0
java.lang.ArithmeticException
- if mode
is RoundingMode.UNNECESSARY
and x
is not a power of two@GwtIncompatible public static int log10(java.math.BigInteger x, java.math.RoundingMode mode)
x
, rounded according to the specified rounding mode.java.lang.IllegalArgumentException
- if x <= 0
java.lang.ArithmeticException
- if mode
is RoundingMode.UNNECESSARY
and x
is not a power of ten@GwtIncompatible public static java.math.BigInteger sqrt(java.math.BigInteger x, java.math.RoundingMode mode)
x
, rounded with the specified rounding mode.java.lang.IllegalArgumentException
- if x < 0
java.lang.ArithmeticException
- if mode
is RoundingMode.UNNECESSARY
and
sqrt(x)
is not an integer@GwtIncompatible private static java.math.BigInteger sqrtFloor(java.math.BigInteger x)
@GwtIncompatible private static java.math.BigInteger sqrtApproxWithDoubles(java.math.BigInteger x)
@GwtIncompatible public static java.math.BigInteger divide(java.math.BigInteger p, java.math.BigInteger q, java.math.RoundingMode mode)
p
by q
, rounding using the specified
RoundingMode
.java.lang.ArithmeticException
- if q == 0
, or if mode == UNNECESSARY
and a
is not an integer multiple of b
public static java.math.BigInteger factorial(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).
java.lang.IllegalArgumentException
- if n < 0
static java.math.BigInteger listProduct(java.util.List<java.math.BigInteger> nums)
static java.math.BigInteger listProduct(java.util.List<java.math.BigInteger> nums, int start, int end)
public static java.math.BigInteger binomial(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.
java.lang.IllegalArgumentException
- if n < 0
, k < 0
, or k > n
@GwtIncompatible static boolean fitsInLong(java.math.BigInteger x)