public final class LargeInteger extends Number<LargeInteger>
This class represents an immutable integer number of arbitrary size.
It has the following advantages over the
java.math.BigInteger
class:
StackContext
). Note: This class uses ConcurrentContext
to accelerate calculations on multi-cores systems.
Modifier and Type | Field and Description |
---|---|
static LargeInteger |
ONE
The large integer representing the multiplicative identity.
|
static <any> |
PRIME_CERTAINTY
Holds the certainty required when testing for primality
(default
100 , the probability for a composite to
pass the primality test is less than 2-100 ). |
static LargeInteger |
ZERO
The large integer representing the additive identity.
|
Modifier and Type | Method and Description |
---|---|
LargeInteger |
abs()
Returns the absolute value of this large integer.
|
int |
bitLength()
Returns the minimal number of bits to represent this large integer
in the minimal two's-complement (sign excluded).
|
int |
compareTo(LargeInteger that)
Compares two large integers numerically.
|
int |
compareTo(long value)
Compares this large integer to the specified
long value. |
LargeInteger |
copy()
Returns a copy of this number
allocated
by the calling thread (possibly on the stack). |
int |
digitLength()
Returns the minimal number of decimal digits necessary to represent
this large integer (sign excluded).
|
LargeInteger |
divide(int divisor)
Returns this large integer divided by the specified
int
divisor. |
LargeInteger |
divide(LargeInteger that)
Returns this large integer divided by the one specified (integer
division).
|
double |
doubleValue()
Returns the value of this large integer as a
double . |
boolean |
equals(long value)
Compares this large integer against the specified
long
value. |
boolean |
equals(java.lang.Object that)
Compares this large integer against the specified object.
|
static java.lang.Appendable |
format(LargeInteger li,
int radix,
java.lang.Appendable out)
Formats the specified large integer in the specified radix and into
the specified
Appendable argument. |
LargeInteger |
gcd(LargeInteger that)
Returns the greatest common divisor of this large integer and
the one specified.
|
int |
getLowestSetBit()
Returns the index of the lowest-order one bit in this large integer
or
-1 if this.equals(ZERO) . |
LargeInteger |
getRemainder()
Returns the final undivided part after division that is less or of
lower degree than the divisor.
|
int |
hashCode()
Returns the hash code for this large integer number.
|
LargeInteger |
inverseScaled(int precision)
Returns a scaled approximation of
1 / this . |
boolean |
isEven()
Indicates if this large integer is an even number.
|
boolean |
isLargerThan(LargeInteger that)
Indicates if this large integer is larger than the one
specified in absolute value.
|
boolean |
isNegative()
Indicates if this large integer is less than
ZERO . |
boolean |
isOdd()
Indicates if this large integer is an odd number.
|
boolean |
isPositive()
|
boolean |
isPowerOfTwo()
Indicates if this number is a power of two (equals to 2
(
bitLength() - 1)). |
boolean |
isProbablyPrime()
Indicates if this large integer is probably prime.
|
boolean |
isZero()
Indicates if this large integer is equal to
ZERO . |
long |
longValue()
Returns the low order bits of this large integer as a
long . |
LargeInteger |
minus(LargeInteger that)
Returns the difference between this large integer and the one
specified.
|
LargeInteger |
minus(long value)
Returns the difference between this large integer and the specified
value
|
LargeInteger |
mod(LargeInteger m)
Returns this large integer modulo the specified large integer.
|
LargeInteger |
modInverse(LargeInteger m)
Returns the large integer whose value is
(this-1 mod m)
. |
LargeInteger |
modPow(LargeInteger exp,
LargeInteger m)
Returns this large integer raised at the specified exponent modulo
the specified modulus.
|
LargeInteger |
opposite()
Returns the opposite of this large integer.
|
static LargeInteger |
parse(java.lang.CharSequence csq,
int radix,
javolution.text.TextFormat.Cursor cursor)
Parses the specified character sequence from the specified position
as a large integer in the specified radix.
|
LargeInteger |
plus(LargeInteger that)
Returns the sum of this large integer with the one specified.
|
LargeInteger |
plus(long value)
Returns the sum of this large integer with the specified
long integer (convenience method) |
LargeInteger |
remainder(LargeInteger that)
Returns the remainder of the division of this large integer with
the one specified (convenience method equivalent to
this.divide(that).getRemainder() ). |
LargeInteger |
shiftLeft(int n)
Returns the value of this large integer after performing a binary
shift to left.
|
LargeInteger |
shiftRight(int n)
Returns the value of this large integer after performing a binary
shift to right with sign extension
(-1 >> 1 == -1) . |
LargeInteger |
sqrt()
Returns the integer square root of this integer.
|
LargeInteger |
times(LargeInteger that)
Returns the product of this large integer with the one specified.
|
LargeInteger |
times(long multiplier)
Returns the product of this large integer with the specified
long multiplier. |
LargeInteger |
times10pow(int n)
Returns the value of this large integer after multiplication by
a power of ten.
|
LargeInteger |
times2pow(int n)
Returns the value of this large integer after multiplication by
a power of two.
|
int |
toByteArray(byte[] bytes,
int offset)
Returns the two's-complement binary representation of this
large integer.
|
javolution.text.Text |
toText()
Returns the text representation of this number using the current
format . |
javolution.text.Text |
toText(int radix)
Returns the text representation of this number in the specified radix.
|
static LargeInteger |
valueOf(java.math.BigInteger bigInteger)
Returns the large integer corresponding to the specified
java.math.BigInteger instance. |
static LargeInteger |
valueOf(byte[] bytes,
int offset,
int length)
Returns the large integer of specified two's-complement binary
representation.
|
static LargeInteger |
valueOf(java.lang.CharSequence csq)
Returns the large integer for the specified character sequence
using the current
format . |
static LargeInteger |
valueOf(java.lang.CharSequence csq,
int radix)
Returns the large integer for the specified character sequence in
the specified radix.
|
static LargeInteger |
valueOf(long value)
Returns the large integer of specified
long value. |
byteValue, floatValue, intValue, isGreaterThan, isLessThan, pow, shortValue, toString
public static final <any> PRIME_CERTAINTY
100
, the probability for a composite to
pass the primality test is less than 2-100
).public static final LargeInteger ZERO
public static final LargeInteger ONE
public static LargeInteger valueOf(long value)
long
value.value
- the long
value.public static LargeInteger valueOf(byte[] bytes, int offset, int length)
bytes
- the binary representation (two's-complement).offset
- the offset at which to start reading the bytes.length
- the maximum number of bytes to read.java.lang.IndexOutOfBoundsException
- if offset + length > bytes.length
toByteArray(byte[], int)
public int toByteArray(byte[] bytes, int offset)
bytes
- the bytes to hold the binary representation
(two's-complement) of this large integer.offset
- the offset at which to start writing the bytes.java.lang.IndexOutOfBoundsException
- if bytes.length < (bitLength() >> 3) + 1
valueOf(byte[], int, int)
,
bitLength()
public static LargeInteger valueOf(java.lang.CharSequence csq)
format
.csq
- the character sequence to parse.TextFormat.getInstance(LargeInteger.class).parse(csq)
java.lang.NumberFormatException
- if error when parsing.public static LargeInteger valueOf(java.lang.CharSequence csq, int radix)
csq
- the character sequence to parse.radix
- the radix of the representation.LargeInteger.parse(csq, radix, cursor)
java.lang.NumberFormatException
- if error when parsing.public static LargeInteger valueOf(java.math.BigInteger bigInteger)
java.math.BigInteger
instance.bigInteger
- the big integer instance.public boolean isPositive()
this > ZERO
public boolean isNegative()
ZERO
.this < ZERO
public boolean isZero()
ZERO
.this == ZERO
public boolean isEven()
(this & 1) == ZERO
public boolean isOdd()
(this & 1) != ZERO
public boolean isProbablyPrime()
true
if this large integer is probable prime;
false
otherwise.public int bitLength()
public int digitLength()
public boolean isPowerOfTwo()
bitLength()
- 1)).true
if this number is a power of two;
false
otherwise.public int getLowestSetBit()
-1
if this.equals(ZERO)
.-1
public LargeInteger getRemainder()
divide(org.jscience.mathematics.number.LargeInteger)
operation and is not considered as part of
this large integer (ignored by all methods).public boolean isLargerThan(LargeInteger that)
isLargerThan
in class Number<LargeInteger>
that
- the integer to be compared with.this.abs().compareTo(that.abs()) > 0
.public LargeInteger abs()
|this|
.public LargeInteger opposite()
-this
.public LargeInteger plus(long value)
long
integer (convenience method)value
- the long
integer being added.this + value
.public LargeInteger plus(LargeInteger that)
that
- the integer to be added.this + that
.public LargeInteger minus(LargeInteger that)
minus
in class Number<LargeInteger>
that
- the integer to be subtracted.this - that
.public LargeInteger minus(long value)
value
- the value to be subtracted.this - value
.public LargeInteger times(LargeInteger that)
that
- the large integer multiplier.this · that
.public LargeInteger times(long multiplier)
long
multiplier.multiplier
- the long
multiplier.this · multiplier
.public LargeInteger divide(LargeInteger that)
getRemainder()
.that
- the integer divisor.this / that
and this % that
(getRemainder()
)java.lang.ArithmeticException
- if that.equals(ZERO)
public LargeInteger divide(int divisor)
int
divisor. The remainder of this division is accessible using
getRemainder()
.divisor
- the int
divisor.this / divisor
and this % divisor
(getRemainder()
)java.lang.ArithmeticException
- if divisor == 0
public LargeInteger remainder(LargeInteger that)
this.divide(that).getRemainder()
).that
- the value by which this integer is to be divided, and the
remainder returned.this % that
java.lang.ArithmeticException
- if that.equals(ZERO)
divide(LargeInteger)
public LargeInteger inverseScaled(int precision)
1 / this
.precision
- the requested precision (reciprocal error being ± 1).2(precision + this.bitLength()) / this
java.lang.ArithmeticException
- if this.isZero()
public LargeInteger sqrt()
k such as k^2 <= this < (k + 1)^2
java.lang.ArithmeticException
- if this integer is negative.public LargeInteger mod(LargeInteger m)
Note: The result as the same sign as the divisor unlike the Java remainder (%) operator (which as the same sign as the dividend).
m
- the modulus.this mod m
getRemainder()
public LargeInteger modInverse(LargeInteger m)
(this-1 mod m)
.m
- the modulus.this-1 mod m
.java.lang.ArithmeticException
- m <= 0
, or this integer
has no multiplicative inverse mod m (that is, this integer
is not relatively prime to m).public LargeInteger modPow(LargeInteger exp, LargeInteger m)
exp
- the exponent.m
- the modulus.thisexp mod m
java.lang.ArithmeticException
- m <= 0
modInverse(org.jscience.mathematics.number.LargeInteger)
public LargeInteger gcd(LargeInteger that)
that
- the other number to compute the GCD with.ZERO
if
(this.isZero() && that.isZero())
.public LargeInteger shiftLeft(int n)
n
, may be negative,
in which case this method performs a right shift.n
- the shift distance, in bits.this << n
.shiftRight(int)
public LargeInteger shiftRight(int n)
(-1 >> 1 == -1)
.
The shift distance, n
, may be negative, in which case
this method performs a shiftLeft(int)
.n
- the shift distance, in bits.this >> n
.public LargeInteger times2pow(int n)
shiftLeft(int)
for positive n
; but is different from
shiftRight(int)
for negative n
as no sign
extension is performed (-1 >>> 1 == 0
).n
- the power of 2 exponent.this · 2n
.public LargeInteger times10pow(int n)
n
- the decimal exponent.this · 10n
public boolean equals(java.lang.Object that)
equals
in class Number<LargeInteger>
that
- the object to compare with.true
if the objects are the same; false
otherwise.public boolean equals(long value)
long
value.value
- long
value to compare with.true
if this large integer has the specified value;
false
otherwise.public int hashCode()
hashCode
in class Number<LargeInteger>
public long longValue()
long
.
Note: This conversion can lose information about the overall magnitude of the integer value and may return a result with the opposite sign.
longValue
in class Number<LargeInteger>
long
.public double doubleValue()
double
.doubleValue
in class Number<LargeInteger>
double
.public int compareTo(LargeInteger that)
compareTo
in interface java.lang.Comparable<LargeInteger>
compareTo
in class Number<LargeInteger>
that
- the integer to compare with.that
.java.lang.ClassCastException
- that
is not a
large integer.public int compareTo(long value)
long
value.value
- the long
value to compare with.public LargeInteger copy()
Number
allocated
by the calling thread (possibly on the stack).copy
in interface javolution.lang.ValueType
copy
in class Number<LargeInteger>
public javolution.text.Text toText()
format
.toText
in interface javolution.lang.Realtime
toText
in class Number<LargeInteger>
TextFormat.getInstance(LargeInteger.class).format(this)
public javolution.text.Text toText(int radix)
radix
- the radix of the representation.public static LargeInteger parse(java.lang.CharSequence csq, int radix, javolution.text.TextFormat.Cursor cursor)
csq
- the character sequence to parse.radix
- the radix to be used while parsing.cursor
- the current cursor position (being maintained).java.lang.NumberFormatException
- if error when parsing.public static java.lang.Appendable format(LargeInteger li, int radix, java.lang.Appendable out) throws java.io.IOException
Appendable
argument.li
- the large integer to format.radix
- the radix.out
- the Appendable
to append.Appendable
object.java.lang.IllegalArgumentException
- if radix is not in [2 .. 36] range.java.io.IOException
- if an I/O exception occurs.