public final class ArithmeticUtils extends Object
Math
.Modifier and Type | Method and Description |
---|---|
static int |
addAndCheck(int x,
int y)
Add two integers, checking for overflow.
|
static long |
addAndCheck(long a,
long b)
Add two long integers, checking for overflow.
|
static long |
binomialCoefficient(int n,
int k)
Deprecated.
|
static double |
binomialCoefficientDouble(int n,
int k)
Deprecated.
|
static double |
binomialCoefficientLog(int n,
int k)
Deprecated.
|
static long |
factorial(int n)
Deprecated.
|
static double |
factorialDouble(int n)
Deprecated.
|
static double |
factorialLog(int n)
Deprecated.
|
static int |
gcd(int p,
int q)
Computes the greatest common divisor of the absolute value of two
numbers, using a modified version of the "binary gcd" method.
|
static long |
gcd(long p,
long q)
Gets the greatest common divisor of the absolute value of two numbers,
using the "binary gcd" method which avoids division and modulo
operations.
|
static boolean |
isPowerOfTwo(long n)
Returns true if the argument is a power of two.
|
static int |
lcm(int a,
int b)
Returns the least common multiple of the absolute value of two numbers,
using the formula
lcm(a,b) = (a / gcd(a,b)) * b . |
static long |
lcm(long a,
long b)
Returns the least common multiple of the absolute value of two numbers,
using the formula
lcm(a,b) = (a / gcd(a,b)) * b . |
static int |
mulAndCheck(int x,
int y)
Multiply two integers, checking for overflow.
|
static long |
mulAndCheck(long a,
long b)
Multiply two long integers, checking for overflow.
|
static BigInteger |
pow(BigInteger k,
BigInteger e)
Raise a BigInteger to a BigInteger power.
|
static BigInteger |
pow(BigInteger k,
int e)
Raise a BigInteger to an int power.
|
static BigInteger |
pow(BigInteger k,
long e)
Raise a BigInteger to a long power.
|
static int |
pow(int k,
int e)
Raise an int to an int power.
|
static int |
pow(int k,
long e)
Deprecated.
As of 3.3. Please use
pow(int,int) instead. |
static long |
pow(long k,
int e)
Raise a long to an int power.
|
static long |
pow(long k,
long e)
Deprecated.
As of 3.3. Please use
pow(long,int) instead. |
static long |
stirlingS2(int n,
int k)
Deprecated.
|
static int |
subAndCheck(int x,
int y)
Subtract two integers, checking for overflow.
|
static long |
subAndCheck(long a,
long b)
Subtract two long integers, checking for overflow.
|
public static int addAndCheck(int x, int y) throws MathArithmeticException
x
- an addendy
- an addendx+y
MathArithmeticException
- if the result can not be represented
as an int
.public static long addAndCheck(long a, long b) throws MathArithmeticException
a
- an addendb
- an addenda+b
MathArithmeticException
- if the result can not be represented as an long@Deprecated public static long binomialCoefficient(int n, int k) throws NotPositiveException, NumberIsTooLargeException, MathArithmeticException
CombinatoricsUtils.binomialCoefficient(int, int)
n choose k
", the number of
k
-element subsets that can be selected from an
n
-element set.
Preconditions:
0 <= k <= n
(otherwise
IllegalArgumentException
is thrown)long
. The
largest value of n
for which all coefficients are
< Long.MAX_VALUE
is 66. If the computed value exceeds
Long.MAX_VALUE
an ArithMeticException
is
thrown.n
- the size of the setk
- the size of the subsets to be countedn choose k
NotPositiveException
- if n < 0
.NumberIsTooLargeException
- if k > n
.MathArithmeticException
- if the result is too large to be
represented by a long integer.@Deprecated public static double binomialCoefficientDouble(int n, int k) throws NotPositiveException, NumberIsTooLargeException, MathArithmeticException
CombinatoricsUtils.binomialCoefficientDouble(int, int)
double
representation of the Binomial
Coefficient, "n choose k
", the number of
k
-element subsets that can be selected from an
n
-element set.
Preconditions:
0 <= k <= n
(otherwise
IllegalArgumentException
is thrown)double
. The
largest value of n
for which all coefficients are <
Double.MAX_VALUE is 1029. If the computed value exceeds Double.MAX_VALUE,
Double.POSITIVE_INFINITY is returnedn
- the size of the setk
- the size of the subsets to be countedn choose k
NotPositiveException
- if n < 0
.NumberIsTooLargeException
- if k > n
.MathArithmeticException
- if the result is too large to be
represented by a long integer.@Deprecated public static double binomialCoefficientLog(int n, int k) throws NotPositiveException, NumberIsTooLargeException, MathArithmeticException
CombinatoricsUtils.binomialCoefficientLog(int, int)
log
of the Binomial
Coefficient, "n choose k
", the number of
k
-element subsets that can be selected from an
n
-element set.
Preconditions:
0 <= k <= n
(otherwise
IllegalArgumentException
is thrown)n
- the size of the setk
- the size of the subsets to be countedn choose k
NotPositiveException
- if n < 0
.NumberIsTooLargeException
- if k > n
.MathArithmeticException
- if the result is too large to be
represented by a long integer.@Deprecated public static long factorial(int n) throws NotPositiveException, MathArithmeticException
CombinatoricsUtils.factorial(int)
n
Factorial, the
product of the numbers 1,...,n
.
Preconditions:
n >= 0
(otherwise
IllegalArgumentException
is thrown)long
. The
largest value of n
for which n!
<
Long.MAX_VALUE} is 20. If the computed value exceeds Long.MAX_VALUE
an ArithMeticException
is thrown.n
- argumentn!
MathArithmeticException
- if the result is too large to be represented
by a long
.NotPositiveException
- if n < 0
.MathArithmeticException
- if n > 20
: The factorial value is too
large to fit in a long
.@Deprecated public static double factorialDouble(int n) throws NotPositiveException
CombinatoricsUtils.factorialDouble(int)
n
(the product of the numbers 1 to n), as a
double
.
The result should be small enough to fit into a double
: The
largest n
for which n! < Double.MAX_VALUE
is 170.
If the computed value exceeds Double.MAX_VALUE
,
Double.POSITIVE_INFINITY
is returned.n
- Argument.n!
NotPositiveException
- if n < 0
.@Deprecated public static double factorialLog(int n) throws NotPositiveException
CombinatoricsUtils.factorialLog(int)
n
.n
- Argument.n!
NotPositiveException
- if n < 0
.public static int gcd(int p, int q) throws MathArithmeticException
gcd(Integer.MIN_VALUE, Integer.MIN_VALUE)
,
gcd(Integer.MIN_VALUE, 0)
and
gcd(0, Integer.MIN_VALUE)
throw an
ArithmeticException
, because the result would be 2^31, which
is too large for an int value.gcd(x, x)
, gcd(0, x)
and
gcd(x, 0)
is the absolute value of x
, except
for the special cases above.gcd(0, 0)
is the only one which returns
0
.p
- Number.q
- Number.MathArithmeticException
- if the result cannot be represented as
a non-negative int
value.public static long gcd(long p, long q) throws MathArithmeticException
Gets the greatest common divisor of the absolute value of two numbers, using the "binary gcd" method which avoids division and modulo operations. See Knuth 4.5.2 algorithm B. This algorithm is due to Josef Stein (1961).
Special cases:gcd(Long.MIN_VALUE, Long.MIN_VALUE)
,
gcd(Long.MIN_VALUE, 0L)
and
gcd(0L, Long.MIN_VALUE)
throw an
ArithmeticException
, because the result would be 2^63, which
is too large for a long value.gcd(x, x)
, gcd(0L, x)
and
gcd(x, 0L)
is the absolute value of x
, except
for the special cases above.
gcd(0L, 0L)
is the only one which returns
0L
.p
- Number.q
- Number.MathArithmeticException
- if the result cannot be represented as
a non-negative long
value.public static int lcm(int a, int b) throws MathArithmeticException
Returns the least common multiple of the absolute value of two numbers,
using the formula lcm(a,b) = (a / gcd(a,b)) * b
.
lcm(Integer.MIN_VALUE, n)
and
lcm(n, Integer.MIN_VALUE)
, where abs(n)
is a
power of 2, throw an ArithmeticException
, because the result
would be 2^31, which is too large for an int value.lcm(0, x)
and lcm(x, 0)
is
0
for any x
.
a
- Number.b
- Number.MathArithmeticException
- if the result cannot be represented as
a non-negative int
value.public static long lcm(long a, long b) throws MathArithmeticException
Returns the least common multiple of the absolute value of two numbers,
using the formula lcm(a,b) = (a / gcd(a,b)) * b
.
lcm(Long.MIN_VALUE, n)
and
lcm(n, Long.MIN_VALUE)
, where abs(n)
is a
power of 2, throw an ArithmeticException
, because the result
would be 2^63, which is too large for an int value.lcm(0L, x)
and lcm(x, 0L)
is
0L
for any x
.
a
- Number.b
- Number.MathArithmeticException
- if the result cannot be represented
as a non-negative long
value.public static int mulAndCheck(int x, int y) throws MathArithmeticException
x
- Factor.y
- Factor.x * y
.MathArithmeticException
- if the result can not be
represented as an int
.public static long mulAndCheck(long a, long b) throws MathArithmeticException
a
- Factor.b
- Factor.a * b
.MathArithmeticException
- if the result can not be represented
as a long
.public static int subAndCheck(int x, int y) throws MathArithmeticException
x
- Minuend.y
- Subtrahend.x - y
.MathArithmeticException
- if the result can not be represented
as an int
.public static long subAndCheck(long a, long b) throws MathArithmeticException
a
- Value.b
- Value.a - b
.MathArithmeticException
- if the result can not be represented as a
long
.public static int pow(int k, int e) throws NotPositiveException, MathArithmeticException
k
- Number to raise.e
- Exponent (must be positive or zero).NotPositiveException
- if e < 0
.MathArithmeticException
- if the result would overflow.@Deprecated public static int pow(int k, long e) throws NotPositiveException
pow(int,int)
instead.k
- Number to raise.e
- Exponent (must be positive or zero).NotPositiveException
- if e < 0
.public static long pow(long k, int e) throws NotPositiveException, MathArithmeticException
k
- Number to raise.e
- Exponent (must be positive or zero).NotPositiveException
- if e < 0
.MathArithmeticException
- if the result would overflow.@Deprecated public static long pow(long k, long e) throws NotPositiveException
pow(long,int)
instead.k
- Number to raise.e
- Exponent (must be positive or zero).NotPositiveException
- if e < 0
.public static BigInteger pow(BigInteger k, int e) throws NotPositiveException
k
- Number to raise.e
- Exponent (must be positive or zero).NotPositiveException
- if e < 0
.public static BigInteger pow(BigInteger k, long e) throws NotPositiveException
k
- Number to raise.e
- Exponent (must be positive or zero).NotPositiveException
- if e < 0
.public static BigInteger pow(BigInteger k, BigInteger e) throws NotPositiveException
k
- Number to raise.e
- Exponent (must be positive or zero).NotPositiveException
- if e < 0
.@Deprecated public static long stirlingS2(int n, int k) throws NotPositiveException, NumberIsTooLargeException, MathArithmeticException
CombinatoricsUtils.stirlingS2(int, int)
S(n,k)
", the number of
ways of partitioning an n
-element set into k
non-empty
subsets.
The preconditions are 0 <= k <= n
(otherwise
NotPositiveException
is thrown)
n
- the size of the setk
- the number of non-empty subsetsS(n,k)
NotPositiveException
- if k < 0
.NumberIsTooLargeException
- if k > n
.MathArithmeticException
- if some overflow happens, typically for n exceeding 25 and
k between 20 and n-2 (S(n,n-1) is handled specifically and does not overflow)public static boolean isPowerOfTwo(long n)
n
- the number to testCopyright © 2003–2016 The Apache Software Foundation. All rights reserved.