Package com.google.common.math
Class BigIntegerMath
- java.lang.Object
-
- com.google.common.math.BigIntegerMath
-
@GwtCompatible(emulated=true) public final class BigIntegerMath extends java.lang.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
int
and forlong
can be found inIntMath
andLongMath
respectively.- Since:
- 11.0
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.math.BigInteger
binomial(int n, int k)
Returnsn
choosek
, also known as the binomial coefficient ofn
andk
, 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 tox
.static java.math.BigInteger
divide(java.math.BigInteger p, java.math.BigInteger q, java.math.RoundingMode mode)
Returns the result of dividingp
byq
, rounding using the specifiedRoundingMode
.static java.math.BigInteger
factorial(int n)
Returnsn!
, that is, the product of the firstn
positive integers, or1
ifn == 0
.static java.math.BigInteger
floorPowerOfTwo(java.math.BigInteger x)
Returns the largest power of two less than or equal tox
.static boolean
isPowerOfTwo(java.math.BigInteger x)
Returnstrue
ifx
represents a power of two.static int
log10(java.math.BigInteger x, java.math.RoundingMode mode)
Returns the base-10 logarithm ofx
, rounded according to the specified rounding mode.static int
log2(java.math.BigInteger x, java.math.RoundingMode mode)
Returns the base-2 logarithm ofx
, rounded according to the specified rounding mode.static java.math.BigInteger
sqrt(java.math.BigInteger x, java.math.RoundingMode mode)
Returns the square root ofx
, rounded with the specified rounding mode.
-
-
-
Method Detail
-
ceilingPowerOfTwo
@Beta public static java.math.BigInteger ceilingPowerOfTwo(java.math.BigInteger x)
Returns the smallest power of two greater than or equal tox
. This is equivalent toBigInteger.valueOf(2).pow(log2(x, CEILING))
.- Throws:
java.lang.IllegalArgumentException
- ifx <= 0
- Since:
- 20.0
-
floorPowerOfTwo
@Beta public static java.math.BigInteger floorPowerOfTwo(java.math.BigInteger x)
Returns the largest power of two less than or equal tox
. This is equivalent toBigInteger.valueOf(2).pow(log2(x, FLOOR))
.- Throws:
java.lang.IllegalArgumentException
- ifx <= 0
- Since:
- 20.0
-
isPowerOfTwo
public static boolean isPowerOfTwo(java.math.BigInteger x)
Returnstrue
ifx
represents a power of two.
-
log2
public static int log2(java.math.BigInteger x, java.math.RoundingMode mode)
Returns the base-2 logarithm ofx
, rounded according to the specified rounding mode.- Throws:
java.lang.IllegalArgumentException
- ifx <= 0
java.lang.ArithmeticException
- ifmode
isRoundingMode.UNNECESSARY
andx
is not a power of two
-
log10
@GwtIncompatible public static int log10(java.math.BigInteger x, java.math.RoundingMode mode)
Returns the base-10 logarithm ofx
, rounded according to the specified rounding mode.- Throws:
java.lang.IllegalArgumentException
- ifx <= 0
java.lang.ArithmeticException
- ifmode
isRoundingMode.UNNECESSARY
andx
is not a power of ten
-
sqrt
@GwtIncompatible public static java.math.BigInteger sqrt(java.math.BigInteger x, java.math.RoundingMode mode)
Returns the square root ofx
, rounded with the specified rounding mode.- Throws:
java.lang.IllegalArgumentException
- ifx < 0
java.lang.ArithmeticException
- ifmode
isRoundingMode.UNNECESSARY
andsqrt(x)
is not an integer
-
divide
@GwtIncompatible public static java.math.BigInteger divide(java.math.BigInteger p, java.math.BigInteger q, java.math.RoundingMode mode)
Returns the result of dividingp
byq
, rounding using the specifiedRoundingMode
.- Throws:
java.lang.ArithmeticException
- ifq == 0
, or ifmode == UNNECESSARY
anda
is not an integer multiple ofb
-
factorial
public static java.math.BigInteger factorial(int n)
Returnsn!
, that is, the product of the firstn
positive integers, or1
ifn == 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:
java.lang.IllegalArgumentException
- ifn < 0
-
binomial
public static java.math.BigInteger binomial(int n, int k)
Returnsn
choosek
, also known as the binomial coefficient ofn
andk
, that is,n! / (k! (n - k)!)
.Warning: the result can take as much as O(k log n) space.
- Throws:
java.lang.IllegalArgumentException
- ifn < 0
,k < 0
, ork > n
-
-