Package org.apfloat
Class ApintMath
- java.lang.Object
-
- org.apfloat.ApintMath
-
public class ApintMath extends java.lang.Object
Various mathematical functions for arbitrary precision integers.- Version:
- 1.14.0
-
-
Field Summary
Fields Modifier and Type Field Description private static java.util.Random
RANDOM
-
Constructor Summary
Constructors Modifier Constructor Description private
ApintMath()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static Apint
abs(Apint x)
Absolute value.static Apint
binomial(long n, long k)
Binomial coefficient.static Apint
binomial(long n, long k, int radix)
Binomial coefficient.static Apint
binomial(Apint n, Apint k)
Binomial coefficient.static Apint[]
cbrt(Apint x)
Cube root and remainder.static Apint
copySign(Apint x, Apint y)
Copy sign from one argument to another.static Apint[]
div(Apint x, Apint y)
Quotient and remainder.static Apint
doubleFactorial(long n)
Double factorial function.static Apint
doubleFactorial(long n, int radix)
Double factorial function.static Apint
factorial(long n)
Factorial function.static Apint
factorial(long n, int radix)
Factorial function.static Apint
gcd(Apint a, Apint b)
Greatest common divisor.static Apint
lcm(Apint a, Apint b)
Least common multiple.static Apint
max(Apint x, Apint y)
Returns the greater of the two values.static Apint
min(Apint x, Apint y)
Returns the smaller of the two values.private static Apint
modInverse(Apint a, Apint m)
static Apint
modMultiply(Apint a, Apint b, Apint m)
Modular multiplication.private static Apint
modMultiply(Apint x1, Apint x2, Apint y, Apfloat inverseY)
static Apint
modPow(Apint a, Apint b, Apint m)
Modular power.static Apint
negate(Apint x)
Deprecated.UseApint.negate()
.private static Apint
pochhammer(long n, long m, int radix)
private static Apint
pochhammer(Apint n, Apint m)
static Apint
pow(Apint x, long n)
Integer power.private static Apint
powXMinus1(Apint pow, Apint x, long n)
private static Apint
powXPlus1(Apint pow, Apint x, long n)
static Apint
product(Apint... x)
Product of numbers.static Apint
random(long digits)
Generates a random number.static Apint
random(long digits, int radix)
Generates a random number.static Apint[]
root(Apint x, long n)
Positive integer root and remainder.static Apint
scale(Apint x, long scale)
Multiply by a power of the radix.static Apint[]
sqrt(Apint x)
Square root and remainder.static Apint
sum(Apint... x)
Sum of numbers.
-
-
-
Method Detail
-
pow
public static Apint pow(Apint x, long n) throws java.lang.ArithmeticException, ApfloatRuntimeException
Integer power.- Parameters:
x
- Base of the power operator.n
- Exponent of the power operator.- Returns:
x
to then
:th power, that isxn
.- Throws:
java.lang.ArithmeticException
- If bothx
andn
are zero.ApfloatRuntimeException
-
sqrt
public static Apint[] sqrt(Apint x) throws java.lang.ArithmeticException, ApfloatRuntimeException
Square root and remainder.- Parameters:
x
- The argument.- Returns:
- An array of two apints:
[q, r]
, whereq2 + r = x
. - Throws:
java.lang.ArithmeticException
- Ifx
is negative.ApfloatRuntimeException
-
cbrt
public static Apint[] cbrt(Apint x) throws ApfloatRuntimeException
Cube root and remainder.- Parameters:
x
- The argument.- Returns:
- An array of two apints:
[q, r]
, whereq3 + r = x
. - Throws:
ApfloatRuntimeException
-
root
public static Apint[] root(Apint x, long n) throws java.lang.ArithmeticException, ApfloatRuntimeException
Positive integer root and remainder.Returns the
n
:th root ofx
, that isx1/n
, rounded towards zero.- Parameters:
x
- The argument.n
- Which root to take.- Returns:
- An array of two apints:
[q, r]
, whereqn + r = x
. - Throws:
java.lang.ArithmeticException
- Ifn
andx
are zero, orx
is negative andn
is even.ApfloatRuntimeException
-
powXMinus1
private static Apint powXMinus1(Apint pow, Apint x, long n) throws ApfloatRuntimeException
- Throws:
ApfloatRuntimeException
-
powXPlus1
private static Apint powXPlus1(Apint pow, Apint x, long n) throws ApfloatRuntimeException
- Throws:
ApfloatRuntimeException
-
negate
@Deprecated public static Apint negate(Apint x) throws ApfloatRuntimeException
Deprecated.UseApint.negate()
.Returns an apint whose value is-x
.- Parameters:
x
- The argument.- Returns:
-x
.- Throws:
ApfloatRuntimeException
-
abs
public static Apint abs(Apint x) throws ApfloatRuntimeException
Absolute value.- Parameters:
x
- The argument.- Returns:
- Absolute value of
x
. - Throws:
ApfloatRuntimeException
-
copySign
public static Apint copySign(Apint x, Apint y) throws ApfloatRuntimeException
Copy sign from one argument to another.- Parameters:
x
- The value whose sign is to be adjusted.y
- The value whose sign is to be used.- Returns:
x
with its sign changed to match the sign ofy
.- Throws:
ApfloatRuntimeException
- Since:
- 1.1
-
scale
public static Apint scale(Apint x, long scale) throws ApfloatRuntimeException
Multiply by a power of the radix. Any rounding will occur towards zero.- Parameters:
x
- The argument.scale
- The scaling factor.- Returns:
x * x.radix()scale
.- Throws:
ApfloatRuntimeException
-
div
public static Apint[] div(Apint x, Apint y) throws java.lang.ArithmeticException, ApfloatRuntimeException
Quotient and remainder.- Parameters:
x
- The dividend.y
- The divisor.- Returns:
- An array of two apints:
[quotient, remainder]
, that is[x / y, x % y]
. - Throws:
java.lang.ArithmeticException
- In case the divisor is zero.ApfloatRuntimeException
-
gcd
public static Apint gcd(Apint a, Apint b) throws ApfloatRuntimeException
Greatest common divisor. This method returns a positive number even if one ofa
andb
is negative.- Parameters:
a
- First argument.b
- Second argument.- Returns:
- Greatest common divisor of
a
andb
. - Throws:
ApfloatRuntimeException
-
lcm
public static Apint lcm(Apint a, Apint b) throws ApfloatRuntimeException
Least common multiple. This method returns a positive number even if one ofa
andb
is negative.- Parameters:
a
- First argument.b
- Second argument.- Returns:
- Least common multiple of
a
andb
. - Throws:
ApfloatRuntimeException
-
modMultiply
public static Apint modMultiply(Apint a, Apint b, Apint m) throws ApfloatRuntimeException
Modular multiplication. Returnsa * b % m
- Parameters:
a
- First argument.b
- Second argument.m
- Modulus.- Returns:
a * b mod m
- Throws:
ApfloatRuntimeException
-
modMultiply
private static Apint modMultiply(Apint x1, Apint x2, Apint y, Apfloat inverseY) throws ApfloatRuntimeException
- Throws:
ApfloatRuntimeException
-
modPow
public static Apint modPow(Apint a, Apint b, Apint m) throws java.lang.ArithmeticException, ApfloatRuntimeException
Modular power.- Parameters:
a
- Base.b
- Exponent.m
- Modulus.- Returns:
ab mod m
- Throws:
java.lang.ArithmeticException
- If the exponent is negative but the GCD ofa
andm
is not 1 and the modular inverse does not exist.ApfloatRuntimeException
-
modInverse
private static Apint modInverse(Apint a, Apint m) throws java.lang.ArithmeticException, ApfloatRuntimeException
- Throws:
java.lang.ArithmeticException
ApfloatRuntimeException
-
factorial
public static Apint factorial(long n) throws java.lang.ArithmeticException, java.lang.NumberFormatException, ApfloatRuntimeException
Factorial function. Uses the default radix.- Parameters:
n
- The number whose factorial is to be calculated. Should be non-negative.- Returns:
n!
- Throws:
java.lang.ArithmeticException
- Ifn
is negative.java.lang.NumberFormatException
- If the default radix is not valid.ApfloatRuntimeException
- Since:
- 1.1
-
factorial
public static Apint factorial(long n, int radix) throws java.lang.ArithmeticException, java.lang.NumberFormatException, ApfloatRuntimeException
Factorial function. Returns a number in the specified radix.- Parameters:
n
- The number whose factorial is to be calculated. Should be non-negative.radix
- The radix to use.- Returns:
n!
- Throws:
java.lang.ArithmeticException
- Ifn
is negative.java.lang.NumberFormatException
- If the radix is not valid.ApfloatRuntimeException
- Since:
- 1.1
-
doubleFactorial
public static Apint doubleFactorial(long n) throws java.lang.ArithmeticException, java.lang.NumberFormatException, ApfloatRuntimeException
Double factorial function. Uses the default radix.- Parameters:
n
- The number whose double factorial is to be calculated. Should be non-negative.- Returns:
n!!
- Throws:
java.lang.ArithmeticException
- Ifn
is negative.java.lang.NumberFormatException
- If the default radix is not valid.ApfloatRuntimeException
- Since:
- 1.14.0
-
doubleFactorial
public static Apint doubleFactorial(long n, int radix) throws java.lang.ArithmeticException, java.lang.NumberFormatException, ApfloatRuntimeException
Double factorial function. Returns a number in the specified radix.- Parameters:
n
- The number whose double factorial is to be calculated. Should be non-negative.radix
- The radix to use.- Returns:
n!!
- Throws:
java.lang.ArithmeticException
- Ifn
is negative.java.lang.NumberFormatException
- If the radix is not valid.ApfloatRuntimeException
- Since:
- 1.14.0
-
binomial
public static Apint binomial(long n, long k) throws ApfloatRuntimeException
Binomial coefficient. Uses the default radix.- Parameters:
n
- The first argument.k
- The second argument.- Returns:
- Throws:
ApfloatRuntimeException
- Since:
- 1.11.0
-
binomial
public static Apint binomial(long n, long k, int radix) throws java.lang.NumberFormatException, ApfloatRuntimeException
Binomial coefficient. Uses the specified radix.- Parameters:
n
- The first argument.k
- The second argument.radix
- The radix.- Returns:
- Throws:
java.lang.NumberFormatException
- If the radix is not valid.ApfloatRuntimeException
- Since:
- 1.11.0
-
binomial
public static Apint binomial(Apint n, Apint k) throws ApfloatRuntimeException
Binomial coefficient.- Parameters:
n
- The first argument.k
- The second argument.- Returns:
- Throws:
ApfloatRuntimeException
- Since:
- 1.11.0
-
pochhammer
private static Apint pochhammer(long n, long m, int radix)
-
product
public static Apint product(Apint... x) throws ApfloatRuntimeException
Product of numbers. This method may perform significantly better than simply multiplying the numbers sequentially.If there are no arguments, the return value is
1
.- Parameters:
x
- The argument(s).- Returns:
- The product of the given numbers.
- Throws:
ApfloatRuntimeException
- Since:
- 1.3
-
sum
public static Apint sum(Apint... x) throws ApfloatRuntimeException
Sum of numbers.If there are no arguments, the return value is
0
.- Parameters:
x
- The argument(s).- Returns:
- The sum of the given numbers.
- Throws:
ApfloatRuntimeException
- Since:
- 1.3
-
random
public static Apint random(long digits)
Generates a random number. Uses the default radix. Returned values are chosen pseudorandomly with (approximately) uniform distribution from the range0 ≤ x < radixdigits
. The generated random numbers may have leading zeros and may thus not always have exactly the requested number of digis.- Parameters:
digits
- Maximum number of digits in the number.- Returns:
- A random number, uniformly distributed between
0 ≤ x < radixdigits
. - Throws:
java.lang.NumberFormatException
- If the default radix is not valid.java.lang.IllegalArgumentException
- In case the number of specified digits is invalid.- Since:
- 1.9.0
-
random
public static Apint random(long digits, int radix)
Generates a random number. Returned values are chosen pseudorandomly with (approximately) uniform distribution from the range0 ≤ x < radixdigits
. The generated random numbers may have leading zeros and may thus not always have exactly the requested number of digis.- Parameters:
digits
- Maximum number of digits in the number.radix
- The radix in which the number should be generated.- Returns:
- A random number, uniformly distributed between
0 ≤ x < radixdigits
, in baseradix
. - Throws:
java.lang.NumberFormatException
- If the radix is not valid.java.lang.IllegalArgumentException
- In case the number of specified digits is invalid.- Since:
- 1.9.0
-
max
public static Apint max(Apint x, Apint y)
Returns the greater of the two values.- Parameters:
x
- An argument.y
- Another argument.- Returns:
- The greater of the two values.
- Since:
- 1.9.0
-
-