Package org.jscience.mathematics.number
Class LargeInteger
- java.lang.Object
-
- java.lang.Number
-
- org.jscience.mathematics.number.Number<LargeInteger>
-
- org.jscience.mathematics.number.LargeInteger
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Comparable<LargeInteger>,javolution.lang.Immutable,javolution.lang.Realtime,javolution.lang.ValueType,javolution.xml.XMLSerializable,GroupAdditive<LargeInteger>,Ring<LargeInteger>,Structure<LargeInteger>
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.BigIntegerclass:- 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
ConcurrentContextto accelerate calculations on multi-cores systems.- Version:
- 3.3, January 14, 2007
- Author:
- Jean-Marie Dautelle
- See Also:
- Wikipedia: Arbitrary-precision Arithmetic, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static LargeIntegerONEThe large integer representing the multiplicative identity.static javolution.lang.Configurable<java.lang.Integer>PRIME_CERTAINTYHolds the certainty required when testing for primality (default100, the probability for a composite to pass the primality test is less than2-100).static LargeIntegerZEROThe large integer representing the additive identity.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description LargeIntegerabs()Returns the absolute value of this large integer.intbitLength()Returns the minimal number of bits to represent this large integer in the minimal two's-complement (sign excluded).intcompareTo(long value)Compares this large integer to the specifiedlongvalue.intcompareTo(LargeInteger that)Compares two large integers numerically.LargeIntegercopy()Returns a copy of this numberallocatedby the calling thread (possibly on the stack).intdigitLength()Returns the minimal number of decimal digits necessary to represent this large integer (sign excluded).LargeIntegerdivide(int divisor)Returns this large integer divided by the specifiedintdivisor.LargeIntegerdivide(LargeInteger that)Returns this large integer divided by the one specified (integer division).doubledoubleValue()Returns the value of this large integer as adouble.booleanequals(long value)Compares this large integer against the specifiedlongvalue.booleanequals(java.lang.Object that)Compares this large integer against the specified object.static java.lang.Appendableformat(LargeInteger li, int radix, java.lang.Appendable out)Formats the specified large integer in the specified radix and into the specifiedAppendableargument.LargeIntegergcd(LargeInteger that)Returns the greatest common divisor of this large integer and the one specified.intgetLowestSetBit()Returns the index of the lowest-order one bit in this large integer or-1ifthis.equals(ZERO).LargeIntegergetRemainder()Returns the final undivided part after division that is less or of lower degree than the divisor.inthashCode()Returns the hash code for this large integer number.LargeIntegerinverseScaled(int precision)Returns a scaled approximation of1 / this.booleanisEven()Indicates if this large integer is an even number.booleanisLargerThan(LargeInteger that)Indicates if this large integer is larger than the one specified in absolute value.booleanisNegative()Indicates if this large integer is less thanZERO.booleanisOdd()Indicates if this large integer is an odd number.booleanisPositive()booleanisPowerOfTwo()Indicates if this number is a power of two (equals to 2 (bitLength()- 1)).booleanisProbablyPrime()Indicates if this large integer is probably prime.booleanisZero()Indicates if this large integer is equal toZERO.longlongValue()Returns the low order bits of this large integer as along.LargeIntegerminus(long value)Returns the difference between this large integer and the specified valueLargeIntegerminus(LargeInteger that)Returns the difference between this large integer and the one specified.LargeIntegermod(LargeInteger m)Returns this large integer modulo the specified large integer.LargeIntegermodInverse(LargeInteger m)Returns the large integer whose value is(this-1 mod m).LargeIntegermodPow(LargeInteger exp, LargeInteger m)Returns this large integer raised at the specified exponent modulo the specified modulus.LargeIntegeropposite()Returns the opposite of this large integer.static LargeIntegerparse(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.LargeIntegerplus(long value)Returns the sum of this large integer with the specifiedlonginteger (convenience method)LargeIntegerplus(LargeInteger that)Returns the sum of this large integer with the one specified.LargeIntegerremainder(LargeInteger that)Returns the remainder of the division of this large integer with the one specified (convenience method equivalent tothis.divide(that).getRemainder()).LargeIntegershiftLeft(int n)Returns the value of this large integer after performing a binary shift to left.LargeIntegershiftRight(int n)Returns the value of this large integer after performing a binary shift to right with sign extension(-1 >> 1 == -1).LargeIntegersqrt()Returns the integer square root of this integer.LargeIntegertimes(long multiplier)Returns the product of this large integer with the specifiedlongmultiplier.LargeIntegertimes(LargeInteger that)Returns the product of this large integer with the one specified.LargeIntegertimes10pow(int n)Returns the value of this large integer after multiplication by a power of ten.LargeIntegertimes2pow(int n)Returns the value of this large integer after multiplication by a power of two.inttoByteArray(byte[] bytes, int offset)Returns the two's-complement binary representation of this large integer.javolution.text.TexttoText()Returns the text representation of this number using the currentformat.javolution.text.TexttoText(int radix)Returns the text representation of this number in the specified radix.static LargeIntegervalueOf(byte[] bytes, int offset, int length)Returns the large integer of specified two's-complement binary representation.static LargeIntegervalueOf(long value)Returns the large integer of specifiedlongvalue.static LargeIntegervalueOf(java.lang.CharSequence csq)Returns the large integer for the specified character sequence using the currentformat.static LargeIntegervalueOf(java.lang.CharSequence csq, int radix)Returns the large integer for the specified character sequence in the specified radix.static LargeIntegervalueOf(java.math.BigInteger bigInteger)Returns the large integer corresponding to the specifiedjava.math.BigIntegerinstance.-
Methods inherited from class org.jscience.mathematics.number.Number
byteValue, floatValue, intValue, isGreaterThan, isLessThan, pow, shortValue, toString
-
-
-
-
Field Detail
-
PRIME_CERTAINTY
public static final javolution.lang.Configurable<java.lang.Integer> 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
public static final LargeInteger ZERO
The large integer representing the additive identity.
-
ONE
public static final LargeInteger ONE
The large integer representing the multiplicative identity.
-
-
Method Detail
-
valueOf
public static LargeInteger valueOf(long value)
Returns the large integer of specifiedlongvalue.- Parameters:
value- thelongvalue.- Returns:
- the corresponding large integer number.
-
valueOf
public static LargeInteger valueOf(byte[] bytes, int offset, int length)
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:
java.lang.IndexOutOfBoundsException- ifoffset + length > bytes.length- See Also:
toByteArray(byte[], int)
-
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:
java.lang.IndexOutOfBoundsException- ifbytes.length < (bitLength() >> 3) + 1- See Also:
valueOf(byte[], int, int),bitLength()
-
valueOf
public static LargeInteger valueOf(java.lang.CharSequence csq)
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:
java.lang.NumberFormatException- if error when parsing.
-
valueOf
public static LargeInteger valueOf(java.lang.CharSequence csq, int radix)
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:
java.lang.NumberFormatException- if error when parsing.
-
valueOf
public static LargeInteger valueOf(java.math.BigInteger bigInteger)
Returns the large integer corresponding to the specifiedjava.math.BigIntegerinstance.- 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:
trueif this large integer is probable prime;falseotherwise.
-
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:
trueif this number is a power of two;falseotherwise.
-
getLowestSetBit
public int getLowestSetBit()
Returns the index of the lowest-order one bit in this large integer or-1ifthis.equals(ZERO).- Returns:
- the index of the rightmost bit set or
-1
-
getRemainder
public LargeInteger 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
public boolean isLargerThan(LargeInteger that)
Indicates if this large integer is larger than the one specified in absolute value.- Specified by:
isLargerThanin classNumber<LargeInteger>- Parameters:
that- the integer to be compared with.- Returns:
this.abs().compareTo(that.abs()) > 0.
-
abs
public LargeInteger abs()
Returns the absolute value of this large integer.- Returns:
|this|.
-
opposite
public LargeInteger opposite()
Returns the opposite of this large integer.- Returns:
-this.
-
plus
public LargeInteger plus(long value)
Returns the sum of this large integer with the specifiedlonginteger (convenience method)- Parameters:
value- thelonginteger being added.- Returns:
this + value.
-
plus
public LargeInteger plus(LargeInteger that)
Returns the sum of this large integer with the one specified.- Parameters:
that- the integer to be added.- Returns:
this + that.
-
minus
public LargeInteger minus(LargeInteger that)
Returns the difference between this large integer and the one specified.- Overrides:
minusin classNumber<LargeInteger>- Parameters:
that- the integer to be subtracted.- Returns:
this - that.
-
minus
public LargeInteger minus(long value)
Returns the difference between this large integer and the specified value- Parameters:
value- the value to be subtracted.- Returns:
this - value.
-
times
public LargeInteger times(LargeInteger that)
Returns the product of this large integer with the one specified.- Parameters:
that- the large integer multiplier.- Returns:
this · that.
-
times
public LargeInteger times(long multiplier)
Returns the product of this large integer with the specifiedlongmultiplier.- Parameters:
multiplier- thelongmultiplier.- Returns:
this · multiplier.
-
divide
public LargeInteger divide(LargeInteger that)
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 / thatandthis % that(getRemainder())- Throws:
java.lang.ArithmeticException- ifthat.equals(ZERO)
-
divide
public LargeInteger divide(int divisor)
Returns this large integer divided by the specifiedintdivisor. The remainder of this division is accessible usinggetRemainder().- Parameters:
divisor- theintdivisor.- Returns:
this / divisorandthis % divisor(getRemainder())- Throws:
java.lang.ArithmeticException- ifdivisor == 0
-
remainder
public LargeInteger remainder(LargeInteger that)
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:
java.lang.ArithmeticException- ifthat.equals(ZERO)- See Also:
divide(LargeInteger)
-
inverseScaled
public LargeInteger inverseScaled(int precision)
Returns a scaled approximation of1 / this.- Parameters:
precision- the requested precision (reciprocal error being ± 1).- Returns:
2(precision + this.bitLength()) / this- Throws:
java.lang.ArithmeticException- ifthis.isZero()
-
sqrt
public LargeInteger sqrt()
Returns the integer square root of this integer.- Returns:
ksuch ask^2 <= this < (k + 1)^2- Throws:
java.lang.ArithmeticException- if this integer is negative.
-
mod
public LargeInteger mod(LargeInteger m)
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:
getRemainder()
-
modInverse
public LargeInteger modInverse(LargeInteger m)
Returns the large integer whose value is(this-1 mod m).- Parameters:
m- the modulus.- Returns:
this-1 mod m.- Throws:
java.lang.ArithmeticException-m <= 0, or this integer has no multiplicative inverse mod m (that is, this integer is not relatively prime to m).
-
modPow
public LargeInteger modPow(LargeInteger exp, LargeInteger m)
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:
java.lang.ArithmeticException-m <= 0- See Also:
modInverse(org.jscience.mathematics.number.LargeInteger)
-
gcd
public LargeInteger gcd(LargeInteger that)
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
ZEROif(this.isZero() && that.isZero()).
-
shiftLeft
public LargeInteger shiftLeft(int n)
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(int)
-
shiftRight
public LargeInteger shiftRight(int n)
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
public LargeInteger times2pow(int n)
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 negativenas no sign extension is performed (-1 >>> 1 == 0).- Parameters:
n- the power of 2 exponent.- Returns:
this · 2n.
-
times10pow
public LargeInteger times10pow(int n)
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
public boolean equals(java.lang.Object that)
Compares this large integer against the specified object.- Specified by:
equalsin classNumber<LargeInteger>- Parameters:
that- the object to compare with.- Returns:
trueif the objects are the same;falseotherwise.
-
equals
public boolean equals(long value)
Compares this large integer against the specifiedlongvalue.- Parameters:
value-longvalue to compare with.- Returns:
trueif this large integer has the specified value;falseotherwise.
-
hashCode
public int hashCode()
Returns the hash code for this large integer number.- Specified by:
hashCodein 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:
longValuein 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:
doubleValuein classNumber<LargeInteger>- Returns:
- the numeric value represented by this integer after conversion
to type
double.
-
compareTo
public int compareTo(LargeInteger that)
Compares two large integers numerically.- Specified by:
compareToin interfacejava.lang.Comparable<LargeInteger>- Specified by:
compareToin 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:
java.lang.ClassCastException-thatis not a large integer.
-
compareTo
public int compareTo(long value)
Compares this large integer to the specifiedlongvalue.- Parameters:
value- thelongvalue to compare with.- Returns:
- -1, 0 or 1 as this integer is numerically less than, equal to, or greater than the specified value.
-
copy
public LargeInteger copy()
Description copied from class:NumberReturns a copy of this numberallocatedby the calling thread (possibly on the stack).- Specified by:
copyin interfacejavolution.lang.ValueType- Specified by:
copyin 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:
toTextin interfacejavolution.lang.Realtime- Specified by:
toTextin 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(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.- 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:
java.lang.NumberFormatException- if error when parsing.
-
format
public static java.lang.Appendable format(LargeInteger li, int radix, java.lang.Appendable out) throws java.io.IOException
Formats the specified large integer in the specified radix and into the specifiedAppendableargument.- Parameters:
li- the large integer to format.radix- the radix.out- theAppendableto append.- Returns:
- the specified
Appendableobject. - Throws:
java.lang.IllegalArgumentException- if radix is not in [2 .. 36] range.java.io.IOException- if an I/O exception occurs.
-
-