Package org.jscience.mathematics.number
Class LargeInteger
- All Implemented Interfaces:
Serializable
,Comparable<LargeInteger>
,javolution.lang.Immutable
,javolution.lang.Realtime
,javolution.lang.ValueType
,javolution.xml.XMLSerializable
,GroupAdditive<LargeInteger>
,Ring<LargeInteger>
,Structure<LargeInteger>
This class represents an immutable integer number of arbitrary size.
It has the following advantages over the
java.math.BigInteger
class:
- Optimized for 64 bits architectures. But still runs significantly faster on 32 bits processors.
- Real-time compliant for improved performance and predictability
(no garbage generated when executing in
StackContext
). - Improved algorithms (e.g. Concurrent Karabutsa multiplication in O(nLog3) instead of O(n2).
Note: This class uses ConcurrentContext
to accelerate calculations on multi-cores systems.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final LargeInteger
The large integer representing the multiplicative identity.static final javolution.lang.Configurable<Integer>
Holds the certainty required when testing for primality (default100
, the probability for a composite to pass the primality test is less than2-100
).static final LargeInteger
The large integer representing the additive identity. -
Method Summary
Modifier and TypeMethodDescriptionabs()
Returns the absolute value of this large integer.int
Returns the minimal number of bits to represent this large integer in the minimal two's-complement (sign excluded).int
compareTo
(long value) Compares this large integer to the specifiedlong
value.int
compareTo
(LargeInteger that) Compares two large integers numerically.copy()
Returns a copy of this numberallocated
by the calling thread (possibly on the stack).int
Returns the minimal number of decimal digits necessary to represent this large integer (sign excluded).divide
(int divisor) Returns this large integer divided by the specifiedint
divisor.divide
(LargeInteger that) Returns this large integer divided by the one specified (integer division).double
Returns the value of this large integer as adouble
.boolean
equals
(long value) Compares this large integer against the specifiedlong
value.boolean
Compares this large integer against the specified object.static Appendable
format
(LargeInteger li, int radix, Appendable out) Formats the specified large integer in the specified radix and into the specifiedAppendable
argument.gcd
(LargeInteger that) Returns the greatest common divisor of this large integer and the one specified.int
Returns the index of the lowest-order one bit in this large integer or-1
ifthis.equals(ZERO)
.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.inverseScaled
(int precision) Returns a scaled approximation of1 / 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
Indicates if this large integer is less thanZERO
.boolean
isOdd()
Indicates if this large integer is an odd number.boolean
boolean
Indicates if this number is a power of two (equals to 2 (bitLength()
- 1)).boolean
Indicates if this large integer is probably prime.boolean
isZero()
Indicates if this large integer is equal toZERO
.long
Returns the low order bits of this large integer as along
.minus
(long value) Returns the difference between this large integer and the specified valueminus
(LargeInteger that) Returns the difference between this large integer and the one specified.mod
(LargeInteger m) Returns this large integer modulo the specified large integer.Returns the large integer whose value is(this-1 mod m)
.modPow
(LargeInteger exp, LargeInteger m) Returns this large integer raised at the specified exponent modulo the specified modulus.opposite()
Returns the opposite of this large integer.static LargeInteger
parse
(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.plus
(long value) Returns the sum of this large integer with the specifiedlong
integer (convenience method)plus
(LargeInteger that) Returns the sum of this large integer with the one specified.remainder
(LargeInteger that) Returns the remainder of the division of this large integer with the one specified (convenience method equivalent tothis.divide(that).getRemainder()
).shiftLeft
(int n) Returns the value of this large integer after performing a binary shift to left.shiftRight
(int n) Returns the value of this large integer after performing a binary shift to right with sign extension(-1 >> 1 == -1)
.sqrt()
Returns the integer square root of this integer.times
(long multiplier) Returns the product of this large integer with the specifiedlong
multiplier.times
(LargeInteger that) Returns the product of this large integer with the one specified.times10pow
(int n) Returns the value of this large integer after multiplication by a power of ten.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 currentformat
.javolution.text.Text
toText
(int radix) Returns the text representation of this number in the specified radix.static LargeInteger
valueOf
(byte[] bytes, int offset, int length) Returns the large integer of specified two's-complement binary representation.static LargeInteger
valueOf
(long value) Returns the large integer of specifiedlong
value.static LargeInteger
valueOf
(CharSequence csq) Returns the large integer for the specified character sequence using the currentformat
.static LargeInteger
valueOf
(CharSequence csq, int radix) Returns the large integer for the specified character sequence in the specified radix.static LargeInteger
valueOf
(BigInteger bigInteger) Returns the large integer corresponding to the specifiedjava.math.BigInteger
instance.Methods inherited from class org.jscience.mathematics.number.Number
byteValue, floatValue, intValue, isGreaterThan, isLessThan, pow, shortValue, toString
-
Field Details
-
PRIME_CERTAINTY
Holds the certainty required when testing for primality (default100
, the probability for a composite to pass the primality test is less than2-100
). -
ZERO
The large integer representing the additive identity. -
ONE
The large integer representing the multiplicative identity.
-
-
Method Details
-
valueOf
Returns the large integer of specifiedlong
value.- Parameters:
value
- thelong
value.- Returns:
- the corresponding large integer number.
-
valueOf
Returns the large integer of specified two's-complement binary representation. The input array is assumed to be in big-endian byte-order: the most significant byte is at the offset position.- Parameters:
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.- Returns:
- the corresponding large integer number.
- Throws:
IndexOutOfBoundsException
- ifoffset + length > bytes.length
- See Also:
-
toByteArray
public int toByteArray(byte[] bytes, int offset) Returns the two's-complement binary representation of this large integer. The output array is in big-endian byte-order: the most significant byte is at the offset position.- Parameters:
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.- Returns:
- the number of bytes written.
- Throws:
IndexOutOfBoundsException
- ifbytes.length < (bitLength() >> 3) + 1
- See Also:
-
valueOf
Returns the large integer for the specified character sequence using the currentformat
.- Parameters:
csq
- the character sequence to parse.- Returns:
TextFormat.getInstance(LargeInteger.class).parse(csq)
- Throws:
NumberFormatException
- if error when parsing.
-
valueOf
Returns the large integer for the specified character sequence in the specified radix.- Parameters:
csq
- the character sequence to parse.radix
- the radix of the representation.- Returns:
LargeInteger.parse(csq, radix, cursor)
- Throws:
NumberFormatException
- if error when parsing.
-
valueOf
Returns the large integer corresponding to the specifiedjava.math.BigInteger
instance.- Parameters:
bigInteger
- the big integer instance.- Returns:
- the large integer having the same value.
-
isPositive
public boolean isPositive()- Returns:
this > ZERO
-
isNegative
public boolean isNegative()Indicates if this large integer is less thanZERO
.- Returns:
this < ZERO
-
isZero
public boolean isZero()Indicates if this large integer is equal toZERO
.- Returns:
this == ZERO
-
isEven
public boolean isEven()Indicates if this large integer is an even number.- Returns:
(this & 1) == ZERO
-
isOdd
public boolean isOdd()Indicates if this large integer is an odd number.- Returns:
(this & 1) != ZERO
-
isProbablyPrime
public boolean isProbablyPrime()Indicates if this large integer is probably prime.- Returns:
true
if this large integer is probable prime;false
otherwise.
-
bitLength
public int bitLength()Returns the minimal number of bits to represent this large integer in the minimal two's-complement (sign excluded).- Returns:
- the length of this integer in bits (sign excluded).
-
digitLength
public int digitLength()Returns the minimal number of decimal digits necessary to represent this large integer (sign excluded).- Returns:
- the maximum number of digits.
-
isPowerOfTwo
public boolean isPowerOfTwo()Indicates if this number is a power of two (equals to 2 (bitLength()
- 1)).- Returns:
true
if this number is a power of two;false
otherwise.
-
getLowestSetBit
public int getLowestSetBit()Returns the index of the lowest-order one bit in this large integer or-1
ifthis.equals(ZERO)
.- Returns:
- the index of the rightmost bit set or
-1
-
getRemainder
Returns the final undivided part after division that is less or of lower degree than the divisor. This value is only set by thedivide(org.jscience.mathematics.number.LargeInteger)
operation and is not considered as part of this large integer (ignored by all methods).- Returns:
- the remainder of the division for which this large integer is the quotient.
-
isLargerThan
Indicates if this large integer is larger than the one specified in absolute value.- Specified by:
isLargerThan
in classNumber<LargeInteger>
- Parameters:
that
- the integer to be compared with.- Returns:
this.abs().compareTo(that.abs()) > 0
.
-
abs
Returns the absolute value of this large integer.- Returns:
|this|
.
-
opposite
Returns the opposite of this large integer.- Returns:
-this
.
-
plus
Returns the sum of this large integer with the specifiedlong
integer (convenience method)- Parameters:
value
- thelong
integer being added.- Returns:
this + value
.
-
plus
Returns the sum of this large integer with the one specified.- Parameters:
that
- the integer to be added.- Returns:
this + that
.
-
minus
Returns the difference between this large integer and the one specified.- Overrides:
minus
in classNumber<LargeInteger>
- Parameters:
that
- the integer to be subtracted.- Returns:
this - that
.
-
minus
Returns the difference between this large integer and the specified value- Parameters:
value
- the value to be subtracted.- Returns:
this - value
.
-
times
Returns the product of this large integer with the one specified.- Parameters:
that
- the large integer multiplier.- Returns:
this · that
.
-
times
Returns the product of this large integer with the specifiedlong
multiplier.- Parameters:
multiplier
- thelong
multiplier.- Returns:
this · multiplier
.
-
divide
Returns this large integer divided by the one specified (integer division). The remainder of this division is accessible usinggetRemainder()
.- Parameters:
that
- the integer divisor.- Returns:
this / that
andthis % that
(getRemainder()
)- Throws:
ArithmeticException
- ifthat.equals(ZERO)
-
divide
Returns this large integer divided by the specifiedint
divisor. The remainder of this division is accessible usinggetRemainder()
.- Parameters:
divisor
- theint
divisor.- Returns:
this / divisor
andthis % divisor
(getRemainder()
)- Throws:
ArithmeticException
- ifdivisor == 0
-
remainder
Returns the remainder of the division of this large integer with the one specified (convenience method equivalent tothis.divide(that).getRemainder()
).- Parameters:
that
- the value by which this integer is to be divided, and the remainder returned.- Returns:
this % that
- Throws:
ArithmeticException
- ifthat.equals(ZERO)
- See Also:
-
inverseScaled
Returns a scaled approximation of1 / this
.- Parameters:
precision
- the requested precision (reciprocal error being ± 1).- Returns:
2(precision + this.bitLength()) / this
- Throws:
ArithmeticException
- ifthis.isZero()
-
sqrt
Returns the integer square root of this integer.- Returns:
k
such as
k^2 <= this < (k + 1)^2
- Throws:
ArithmeticException
- if this integer is negative.
-
mod
Returns this large integer modulo the specified large integer.Note: The result as the same sign as the divisor unlike the Java remainder (%) operator (which as the same sign as the dividend).
- Parameters:
m
- the modulus.- Returns:
this mod m
- See Also:
-
modInverse
Returns the large integer whose value is(this-1 mod m)
.- Parameters:
m
- the modulus.- Returns:
this-1 mod m
.- Throws:
ArithmeticException
-m <= 0
, or this integer has no multiplicative inverse mod m (that is, this integer is not relatively prime to m).
-
modPow
Returns this large integer raised at the specified exponent modulo the specified modulus.- Parameters:
exp
- the exponent.m
- the modulus.- Returns:
thisexp mod m
- Throws:
ArithmeticException
-m <= 0
- See Also:
-
gcd
Returns the greatest common divisor of this large integer and the one specified.- Parameters:
that
- the other number to compute the GCD with.- Returns:
- a positive number or
ZERO
if(this.isZero() && that.isZero())
.
-
shiftLeft
Returns the value of this large integer after performing a binary shift to left. The shift distance,n
, may be negative, in which case this method performs a right shift.- Parameters:
n
- the shift distance, in bits.- Returns:
this << n
.- See Also:
-
shiftRight
Returns the value of this large integer after performing a binary shift to right with sign extension(-1 >> 1 == -1)
. The shift distance,n
, may be negative, in which case this method performs ashiftLeft(int)
.- Parameters:
n
- the shift distance, in bits.- Returns:
this >> n
.
-
times2pow
Returns the value of this large integer after multiplication by a power of two. This method is equivalent toshiftLeft(int)
for positiven
; but is different fromshiftRight(int)
for negativen
as no sign extension is performed (-1 >>> 1 == 0
).- Parameters:
n
- the power of 2 exponent.- Returns:
this · 2n
.
-
times10pow
Returns the value of this large integer after multiplication by a power of ten. For example:[code] LargeInteger billion = LargeInteger.ONE.times10pow(9); // 1E9 LargeInteger million = billion.times10pow(-3);[/code]- Parameters:
n
- the decimal exponent.- Returns:
this · 10n
-
equals
Compares this large integer against the specified object.- Specified by:
equals
in classNumber<LargeInteger>
- Parameters:
that
- the object to compare with.- Returns:
true
if the objects are the same;false
otherwise.
-
equals
public boolean equals(long value) Compares this large integer against the specifiedlong
value.- Parameters:
value
-long
value to compare with.- Returns:
true
if this large integer has the specified value;false
otherwise.
-
hashCode
public int hashCode()Returns the hash code for this large integer number.- Specified by:
hashCode
in classNumber<LargeInteger>
- Returns:
- the hash code value.
-
longValue
public long longValue()Returns the low order bits of this large integer as along
.Note: This conversion can lose information about the overall magnitude of the integer value and may return a result with the opposite sign.
- Specified by:
longValue
in classNumber<LargeInteger>
- Returns:
- the numeric value represented by this integer after conversion
to type
long
.
-
doubleValue
public double doubleValue()Returns the value of this large integer as adouble
.- Specified by:
doubleValue
in classNumber<LargeInteger>
- Returns:
- the numeric value represented by this integer after conversion
to type
double
.
-
compareTo
Compares two large integers numerically.- Specified by:
compareTo
in interfaceComparable<LargeInteger>
- Specified by:
compareTo
in classNumber<LargeInteger>
- Parameters:
that
- the integer to compare with.- Returns:
- -1, 0 or 1 as this integer is numerically less than, equal to,
or greater than
that
. - Throws:
ClassCastException
-that
is not a large integer.
-
compareTo
public int compareTo(long value) Compares this large integer to the specifiedlong
value.- Parameters:
value
- thelong
value to compare with.- Returns:
- -1, 0 or 1 as this integer is numerically less than, equal to, or greater than the specified value.
-
copy
Description copied from class:Number
Returns a copy of this numberallocated
by the calling thread (possibly on the stack).- Specified by:
copy
in interfacejavolution.lang.ValueType
- Specified by:
copy
in classNumber<LargeInteger>
- Returns:
- an identical and independant copy of this number.
-
toText
public javolution.text.Text toText()Returns the text representation of this number using the currentformat
.- Specified by:
toText
in interfacejavolution.lang.Realtime
- Specified by:
toText
in classNumber<LargeInteger>
- Returns:
TextFormat.getInstance(LargeInteger.class).format(this)
-
toText
public javolution.text.Text toText(int radix) Returns the text representation of this number in the specified radix.- Parameters:
radix
- the radix of the representation.- Returns:
- the text representation of this number in the specified radix.
-
parse
public static LargeInteger parse(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.- Parameters:
csq
- the character sequence to parse.radix
- the radix to be used while parsing.cursor
- the current cursor position (being maintained).- Returns:
- the corresponding large integer.
- Throws:
NumberFormatException
- if error when parsing.
-
format
Formats the specified large integer in the specified radix and into the specifiedAppendable
argument.- Parameters:
li
- the large integer to format.radix
- the radix.out
- theAppendable
to append.- Returns:
- the specified
Appendable
object. - Throws:
IllegalArgumentException
- if radix is not in [2 .. 36] range.IOException
- if an I/O exception occurs.
-