Package org.apfloat
Class ApintMath
java.lang.Object
org.apfloat.ApintMath
Various mathematical functions for arbitrary precision integers.
- Version:
- 1.14.0
- Author:
- Mikko Tommila
-
Method Summary
Modifier and TypeMethodDescriptionstatic ApintAbsolute value.static Apintbinomial(long n, long k) Binomial coefficient.static Apintbinomial(long n, long k, int radix) Binomial coefficient.static ApintBinomial coefficient.static Apint[]Cube root and remainder.static ApintCopy sign from one argument to another.static Apint[]Quotient and remainder.static ApintdoubleFactorial(long n) Double factorial function.static ApintdoubleFactorial(long n, int radix) Double factorial function.static Apintfactorial(long n) Factorial function.static Apintfactorial(long n, int radix) Factorial function.static ApintGreatest common divisor.static ApintLeast common multiple.static ApintReturns the greater of the two values.static ApintReturns the smaller of the two values.static ApintmodMultiply(Apint a, Apint b, Apint m) Modular multiplication.static ApintModular power.static ApintDeprecated.static ApintInteger power.static ApintProduct of numbers.static Apintrandom(long digits) Generates a random number.static Apintrandom(long digits, int radix) Generates a random number.static Apint[]Positive integer root and remainder.static ApintMultiply by a power of the radix.static Apint[]Square root and remainder.static ApintSum of numbers.
-
Method Details
-
pow
Integer power.- Parameters:
x- Base of the power operator.n- Exponent of the power operator.- Returns:
xto then:th power, that isxn.- Throws:
ArithmeticException- If bothxandnare zero.ApfloatRuntimeException
-
sqrt
Square root and remainder.- Parameters:
x- The argument.- Returns:
- An array of two apints:
[q, r], whereq2 + r = x. - Throws:
ArithmeticException- Ifxis negative.ApfloatRuntimeException
-
cbrt
Cube root and remainder.- Parameters:
x- The argument.- Returns:
- An array of two apints:
[q, r], whereq3 + r = x. - Throws:
ApfloatRuntimeException
-
root
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:
ArithmeticException- Ifnandxare zero, orxis negative andnis even.ApfloatRuntimeException
-
negate
Deprecated.UseApint.negate().Returns an apint whose value is-x.- Parameters:
x- The argument.- Returns:
-x.- Throws:
ApfloatRuntimeException
-
abs
Absolute value.- Parameters:
x- The argument.- Returns:
- Absolute value of
x. - Throws:
ApfloatRuntimeException
-
copySign
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:
xwith its sign changed to match the sign ofy.- Throws:
ApfloatRuntimeException- Since:
- 1.1
-
scale
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
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:
ArithmeticException- In case the divisor is zero.ApfloatRuntimeException
-
gcd
Greatest common divisor. This method returns a positive number even if one ofaandbis negative.- Parameters:
a- First argument.b- Second argument.- Returns:
- Greatest common divisor of
aandb. - Throws:
ApfloatRuntimeException
-
lcm
Least common multiple. This method returns a positive number even if one ofaandbis negative.- Parameters:
a- First argument.b- Second argument.- Returns:
- Least common multiple of
aandb. - Throws:
ApfloatRuntimeException
-
modMultiply
Modular multiplication. Returnsa * b % m- Parameters:
a- First argument.b- Second argument.m- Modulus.- Returns:
a * b mod m- Throws:
ApfloatRuntimeException
-
modPow
public static Apint modPow(Apint a, Apint b, Apint m) throws ArithmeticException, ApfloatRuntimeException Modular power.- Parameters:
a- Base.b- Exponent.m- Modulus.- Returns:
ab mod m- Throws:
ArithmeticException- If the exponent is negative but the GCD ofaandmis not 1 and the modular inverse does not exist.ApfloatRuntimeException
-
factorial
public static Apint factorial(long n) throws ArithmeticException, 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:
ArithmeticException- Ifnis negative.NumberFormatException- If the default radix is not valid.ApfloatRuntimeException- Since:
- 1.1
-
factorial
public static Apint factorial(long n, int radix) throws ArithmeticException, 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:
ArithmeticException- Ifnis negative.NumberFormatException- If the radix is not valid.ApfloatRuntimeException- Since:
- 1.1
-
doubleFactorial
public static Apint doubleFactorial(long n) throws ArithmeticException, 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:
ArithmeticException- Ifnis negative.NumberFormatException- If the default radix is not valid.ApfloatRuntimeException- Since:
- 1.14.0
-
doubleFactorial
public static Apint doubleFactorial(long n, int radix) throws ArithmeticException, 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:
ArithmeticException- Ifnis negative.NumberFormatException- If the radix is not valid.ApfloatRuntimeException- Since:
- 1.14.0
-
binomial
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 NumberFormatException, ApfloatRuntimeException Binomial coefficient. Uses the specified radix.- Parameters:
n- The first argument.k- The second argument.radix- The radix.- Returns:
- Throws:
NumberFormatException- If the radix is not valid.ApfloatRuntimeException- Since:
- 1.11.0
-
binomial
Binomial coefficient.- Parameters:
n- The first argument.k- The second argument.- Returns:
- Throws:
ApfloatRuntimeException- Since:
- 1.11.0
-
product
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
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
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:
NumberFormatException- If the default radix is not valid.IllegalArgumentException- In case the number of specified digits is invalid.- Since:
- 1.9.0
-
random
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:
NumberFormatException- If the radix is not valid.IllegalArgumentException- In case the number of specified digits is invalid.- Since:
- 1.9.0
-
max
-
min
-
Apint.negate().