Package gmp

Class Mpz

All Implemented Interfaces:
Serializable, Cloneable, Comparable<Mpz>
Direct Known Subclasses:
MpzRef

public class Mpz extends Object implements Serializable, Comparable<Mpz>, Cloneable
GMP arbitrary precision integers.

This class wraps mpz_t GMP objects.

See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    This value can be returned by bit-searching or bit-counting operations.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Mpz()
    Constructs an uninitialized number.
    Mpz(double d)
    Constructs a number initialized with a double.
    Mpz(int i)
    Constructs a number initialized with an integer.
    Mpz(int sign, byte[] mag)
    Constructs a number from a unsigned stored in a big-endian byte array and a sign.
    Mpz(Mpz v)
    Constructs a copy of its argument.
    Constructs a number initialized from a string representation.
    Mpz(String s, int b)
    Constructs a number initialized from a string representation in base b.
    Constructs a number initialized from a Java big integer.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    abs()
    Replaces this with |this|.
    void
    abs(Mpz arg)
    Stores |arg| into this.
    void
    add(int arg)
    Adds arg to this.
    void
    add(Mpz arg)
    Adds arg to this.
    void
    add(Mpz arg1, int arg2)
    Stores arg1+arg2 into this.
    void
    add(Mpz arg1, Mpz arg2)
    Stores arg1+arg2 into this.
    void
    addMul(Mpz arg1, int arg2)
    Stores this+arg1*arg2 into this.
    void
    addMul(Mpz arg1, Mpz arg2)
    Stores this+arg1*arg2 into this.
    void
    and(Mpz op)
    Stores the bitwise and of this and op into this.
    void
    and(Mpz op1, Mpz op2)
    Stores the bitwise and of op1 and op2 into this.
    Returns this as a Java big integer.
    void
    bin(int n, int k)
    Stores into this the binomial coefficient (n k).
    void
    bin(Mpz n, int k)
    Stores into this the binomial coefficient (n k).
    byte[]
    Returns the magnitude of this.
    int
    cdivQ(int d)
    Divides this by d, rounds towards +oo, and returns the remainder.
    void
    Divides this by d and rounds towards +oo.
    int
    cdivQ(Mpz n, int d)
    Stores the quotient of n/d rounded towards +oo into this, and returns the remainder.
    void
    cdivQ(Mpz n, Mpz d)
    Stores the quotient of n/d rounded towards +oo into this.
    void
    cdivQ2exp(Mpz n, int d)
    Stores the quotient of n/(2^d) rounded towards +oo into this.
    int
    cdivQR(Mpz r, Mpz n, int d)
    Stores the quotient of n/d rounded towards +oo into this, the remainder into r, and returns the remainder.
    void
    cdivQR(Mpz r, Mpz n, Mpz d)
    Stores the quotient of n/d rounded towards +oo into this and the remainder into r.
    int
    cdivR(int d)
    Returns the remainder of of this/d rounded towards +oo.
    int
    cdivR(Mpz n, int d)
    Stores the remainder of n/d rounded towards +oo into this, and returns the remainder.
    void
    cdivR(Mpz n, Mpz d)
    Stores the remainder of n/d rounded towards +oo into this.
    void
    cdivR2exp(Mpz n, int d)
    Stores the remainder of n/(2^d) rounded towards +oo into this.
    Creates a new Mpz initialized with the value of this.
    void
    clrBit(int pos)
    Sets the pos-th bit to 0 in this.
    int
    cmp(double op)
    Returns a positive, null, or negative number when this is respectively strictly greater, equal, or strictly smaller than op.
    int
    cmp(int op)
    Returns a positive, null, or negative number when this is respectively strictly greater, equal, or strictly smaller than op.
    int
    cmp(Mpz op)
    Returns a positive, null, or negative number when this is respectively strictly greater, equal, or strictly smaller than op.
    int
    cmpAbs(double op)
    Returns a positive, null, or negative number when |this| is respectively strictly greater, equal, or strictly smaller than |op|.
    int
    cmpAbs(int op)
    Returns a positive, null, or negative number when |this| is respectively strictly greater, equal, or strictly smaller than |op|.
    int
    cmpAbs(Mpz op)
    Returns a positive, null, or negative number when |this| is respectively strictly greater, equal, or strictly smaller than |op|.
    int
    Compare the value of this and x.
    void
    divExact(int d)
    Divides this by d, assuming that d divides this. d must be positive.
    void
    Divides this by d, assuming that d divides this.
    void
    divExact(Mpz n, int d)
    Stores n/d into this, assuming that d divides n. d must be positive.
    void
    divExact(Mpz n, Mpz d)
    Stores n/d into this, assuming that d divides n.
    double
    Converts the integer to a mantissa and exponent.
    double
    Returns this as a double.
    boolean
    Whether x is a Mpz and has the same value as this.
    void
    fac(int op)
    Stores op!
    int
    fdivQ(int d)
    Divides this by d, rounds towards -oo, and returns the remainder.
    void
    Divides this by d and rounds towards -oo.
    int
    fdivQ(Mpz n, int d)
    Stores the quotient of n/d rounded towards -oo into this, and returns the remainder.
    void
    fdivQ(Mpz n, Mpz d)
    Stores the quotient of n/d rounded towards -oo into this.
    void
    fdivQ2exp(Mpz n, int d)
    Stores the quotient of n/(2^d) rounded towards -oo into this.
    int
    fdivQR(Mpz r, Mpz n, int d)
    Stores the quotient of n/d rounded towards -oo into this, the remainder into r, and returns the remainder.
    void
    fdivQR(Mpz r, Mpz n, Mpz d)
    Stores the quotient of n/d rounded towards -oo into this and the remainder into r.
    int
    fdivR(int d)
    Returns the remainder of of this/d rounded towards -oo.
    int
    fdivR(Mpz n, int d)
    Stores the remainder of n/d rounded towards -oo into this, and returns the remainder.
    void
    fdivR(Mpz n, Mpz d)
    Stores the remainder of n/d rounded towards -oo into this.
    void
    fdivR2exp(Mpz n, int d)
    Stores the remainder of n/(2^d) rounded towards -oo into this.
    void
    fib(int n)
    Stores into this the n-th Fibonacci number.
    void
    fib(Mpz r, int n)
    Stores into this the n-th Fibonacci number, and into r the n-1-th Fibonacci number.
    boolean
    Whether this can fit in an int.
    int
    gcd(Mpz op1, int op2)
    Returns the greatest common divisor of op1 and op2.
    void
    gcd(Mpz op1, Mpz op2)
    Stores into this the greatest common divisor of op1 and op2.
    void
    gcd(Mpz s, Mpz t, Mpz op1, Mpz op2)
    Stores the greatest common divisor of op1 and op2 into this.
    int
    Returns the Hamming distance between this and op.
    int
    Returns a hash of the value of this.
    int
    Returns this as an integer.
    boolean
    invert(Mpz op1, Mpz op2)
    Stores the inverse of op1 modulo op2 into this.
    boolean
    isCongruent(int c, int d)
    Returns true if this equals c modulo d.
    boolean
    Returns true if this equals c modulo d.
    boolean
    isCongruent2exp(Mpz c, int d)
    Returns true if this equals c modulo 2^d.
    boolean
    isDivisible(int d)
    Returns true if d divides this.
    boolean
    Returns true if d divides this.
    boolean
    Returns true if 2^d divides this.
    boolean
    Whether this and x have the same value.
    boolean
    Whether this is even.
    boolean
    Whether this is odd.
    boolean
    Returns true if this has the form a^b for some a and b > 1.
    boolean
    Returns true if this has the form a^2 for some a.
    boolean
    Whether this and x have the same address in memory.
    int
    Returns 2 if this is definitely prime, 1 if it is probably prime, and 0 if it is definitely composite.
    static int
    jacobi(Mpz b, Mpz p)
    Returns the Jacobi symbol of a and b.
    static int
    kronecker(int a, Mpz b)
    Returns the Jacobi symbol with the Kronecker extension.
    static int
    kronecker(Mpz a, int b)
    Returns the Jacobi symbol with the Kronecker extension.
    static int
    kronecker(Mpz a, Mpz b)
    Returns the Jacobi symbol with the Kronecker extension.
    void
    lcm(Mpz op1, int op2)
    Stores into this the least common multiple of op1 and op2.
    void
    lcm(Mpz op1, Mpz op2)
    Stores into this the least common multiple of op1 and op2.
    static int
    legendre(Mpz a, Mpz p)
    Returns the Legendre symbol.
    void
    lucnum(int n)
    Stores into this the n-th Lucas number.
    void
    lucnum(Mpz r, int n)
    Stores into this the n-th Lucas number, and into r the n-1-th Lucas number.
    int
    mod(int d)
    Replaces this with this mod |d|.
    void
    mod(Mpz d)
    Replaces this with this mod |d|.
    int
    mod(Mpz n, int d)
    Stores n mod |d| into this and returns it.
    void
    mod(Mpz n, Mpz d)
    Stores n mod |d| into this.
    void
    mul(int arg)
    Multiplies this by arg.
    void
    mul(Mpz arg)
    Multiplies this by arg.
    void
    mul(Mpz arg1, int arg2)
    Stores arg1*arg2 into this.
    void
    mul(Mpz arg1, Mpz arg2)
    Stores arg1*arg2 into this.
    void
    mul2exp(int arg)
    Multiplies this by 2^arg.
    void
    mul2exp(Mpz arg1, int arg2)
    Stores arg1*2^arg2 into this.
    void
    neg()
    Replaces this with -this.
    void
    neg(Mpz arg)
    Stores -arg into this.
    void
    Stores into this the next prime greater than op.
    void
    not()
    Stores the bitwise complement of this into this.
    void
    not(Mpz op)
    Stores the bitwise complement of op into this.
    void
    notBit(int pos)
    Inverts the pos-th bit in this.
    void
    or(Mpz op)
    Stores the bitwise or of this and op into this.
    void
    or(Mpz op1, Mpz op2)
    Stores the bitwise or of op1 and op2 into this.
    int
    Returns the number of bits set to 1 in this.
    void
    pow(int e)
    Takes the e-th power of this.
    void
    pow(int b, int e)
    Stores b^e into this.
    void
    pow(Mpz b, int e)
    Stores b^e into this.
    void
    pow(Mpz b, int e, Mpz m)
    Stores b^e mod m into this.
    void
    pow(Mpz b, Mpz e, Mpz m)
    Stores b^e mod m into this.
    void
    Stores into this a uniformly distributed random number in 0..n-1.
    void
    randomBits(RandState r, int n)
    Stores into this a uniformly distributed random number with n bits (i.e., in 0..2^n-1).
    int
    Removes from this all occurrences of factor f.
    int
    remove(Mpz op, Mpz f)
    Stores into this op with all occurrences of factor f removed.
    boolean
    root(int n)
    Takes the n-th root of this, truncated.
    boolean
    root(Mpz op, int n)
    Stores the n-th root of op into this, truncated.
    void
    root(Mpz r, Mpz op, int n)
    Stores the n-th root of op into this, truncated, and the remainder op-this^n into r.
    void
    Stores into this a random number with n bits, and long strings of zeros and ones in its binary representation.
    int
    scan0(int pos)
    Returns the position of the first bit set to 0 at or after position pos.
    int
    scan1(int pos)
    Returns the position of the first bit set to 1 at or after position pos.
    void
    set(double d)
    Sets this to a double.
    void
    set(int i)
    Sets this to an integer.
    void
    set(int sign, byte[] mag)
    Sets the value of this given a magnitude and a sign.
    void
    set(Mpz v)
    Copies the argument into this.
    void
    Sets this to the String s.
    void
    set(String s, int b)
    Sets this to the String s expressed in base b.
    void
    Sets this to a Java big integer.
    void
    setBit(int pos)
    Sets the pos-th bit to 1 in this.
    int
    sgn()
    Returns +1, 0, or -1 when this is respectively strictly positive, null, or strictly negative.
    int
    Returns the size (in GMP limbs) of the internal representation of this.
    int
    sizeInBase(int b)
    Returns the number of digits necessary to represent |this| in base b.
    void
    Takes the truncated square root of this.
    void
    sqrt(Mpz op)
    Stores the truncated square root of op into this.
    void
    sqrt(Mpz r, Mpz op)
    Stores the square root of op into this, truncated, and the remainder op-this^2 into r.
    void
    sub(int arg)
    Subtracts arg from this.
    void
    sub(int arg1, Mpz arg2)
    Stores arg1-arg2 into this.
    void
    sub(Mpz arg)
    Subtracts arg from this.
    void
    sub(Mpz arg1, int arg2)
    Stores arg1-arg2 into this.
    void
    sub(Mpz arg1, Mpz arg2)
    Stores arg1-arg2 into this.
    void
    subMul(Mpz arg1, int arg2)
    Stores this-arg1*arg2 into this.
    void
    subMul(Mpz arg1, Mpz arg2)
    Stores this-arg1*arg2 into this.
    void
    subRev(int arg)
    Replaces this with arg-this.
    int
    tdivQ(int d)
    Divides this by d, rounds towards 0, and returns the remainder.
    void
    Divides this by d and rounds towards 0.
    int
    tdivQ(Mpz n, int d)
    Stores the quotient of n/d rounded towards 0 into this, and returns the remainder.
    void
    tdivQ(Mpz n, Mpz d)
    Stores the quotient of n/d rounded towards 0 into this.
    void
    tdivQ2exp(Mpz n, int d)
    Stores the quotient of n/(2^d) rounded towards 0 into this.
    int
    tdivQR(Mpz r, Mpz n, int d)
    Stores the quotient of n/d rounded towards 0 into this, the remainder into r, and returns the remainder.
    void
    tdivQR(Mpz r, Mpz n, Mpz d)
    Stores the quotient of n/d rounded towards 0 into this and the remainder into r.
    int
    tdivR(int d)
    Returns the remainder of of this/d rounded towards 0.
    int
    tdivR(Mpz n, int d)
    Stores the remainder of n/d rounded towards 0 into this, and returns the remainder.
    void
    tdivR(Mpz n, Mpz d)
    Stores the remainder of n/d rounded towards 0 into this.
    void
    tdivR2exp(Mpz n, int d)
    Stores the remainder of n/(2^d) rounded towards 0 into this.
    Returns a String representation this in base 10.
    toString(int b)
    Returns a String representation of this in base b.
    boolean
    tstBit(int pos)
    Returns true if the pos-th bit in this is 1.
    void
    xor(Mpz op)
    Stores the bitwise xor of this and op into this.
    void
    xor(Mpz op1, Mpz op2)
    Stores the bitwise xor of op1 and op2 into this.

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • infinite

      public static final int infinite
      This value can be returned by bit-searching or bit-counting operations.
      See Also:
  • Constructor Details

    • Mpz

      public Mpz()
      Constructs an uninitialized number.
    • Mpz

      public Mpz(Mpz v)
      Constructs a copy of its argument.
    • Mpz

      public Mpz(int i)
      Constructs a number initialized with an integer.
    • Mpz

      public Mpz(double d)
      Constructs a number initialized with a double.

      The value is truncated to an integer.

    • Mpz

      public Mpz(BigInteger b)
      Constructs a number initialized from a Java big integer.
    • Mpz

      public Mpz(String s, int b)
      Constructs a number initialized from a string representation in base b.
      Parameters:
      b - must be either 0, or between 2 and 62. When b is 0, the leading characters are used to determine the base (0x and 0X for hexadecimal, 0b and 0B for binary, 0 for octal, decimal otherwise).
    • Mpz

      public Mpz(String s)
      Constructs a number initialized from a string representation.

      The leading characters are used to determine whether base: 0x and 0X for hexadecimal, 0b and 0B for binary, 0 for octal, and decimal otherwise.

    • Mpz

      public Mpz(int sign, byte[] mag)
      Constructs a number from a unsigned stored in a big-endian byte array and a sign.
  • Method Details

    • set

      public void set(int sign, byte[] mag)
      Sets the value of this given a magnitude and a sign.

      Used during deserialization.

      mag is the absolute value encoded in a byte array in big endian order.

    • byteArrayValue

      public byte[] byteArrayValue()
      Returns the magnitude of this.

      User during serialization.

      The magnitude is the absolute value, and it is returned as a byte array stored in big-endian order.

    • clone

      public Mpz clone() throws CloneNotSupportedException
      Creates a new Mpz initialized with the value of this.
      Overrides:
      clone in class Object
      Throws:
      CloneNotSupportedException
    • set

      public void set(Mpz v)
      Copies the argument into this.
    • set

      public void set(int i)
      Sets this to an integer.
    • set

      public void set(double d)
      Sets this to a double.

      The value is truncated.

    • set

      public void set(BigInteger b)
      Sets this to a Java big integer.
    • set

      public void set(String s, int b)
      Sets this to the String s expressed in base b.
      See Also:
    • set

      public void set(String s)
      Sets this to the String s.
      See Also:
    • intValue

      public int intValue()
      Returns this as an integer.

      The result is truncated if it does not fit in an integer.

      See Also:
    • doubleValue

      public double doubleValue()
      Returns this as a double.

      If this cannot be represented as a double, the result is truncated.

      Overflows cause infinities to be returned.

    • doubleExponentValue

      public double doubleExponentValue(int[] r)
      Converts the integer to a mantissa and exponent.

      The mantissa m returned is such that 0.5 <= |m| < 1, truncated to fit double.

      The exponent is stored into r[0].

    • bigIntegerValue

      public BigInteger bigIntegerValue()
      Returns this as a Java big integer.
    • toString

      public String toString(int b)
      Returns a String representation of this in base b.
      Parameters:
      b - must be between 2 and 62.
    • toString

      public String toString()
      Returns a String representation this in base 10.
      Overrides:
      toString in class Object
    • add

      public void add(Mpz arg1, Mpz arg2)
      Stores arg1+arg2 into this.
    • add

      public void add(Mpz arg1, int arg2)
      Stores arg1+arg2 into this.
    • add

      public void add(Mpz arg)
      Adds arg to this.
    • add

      public void add(int arg)
      Adds arg to this.
    • sub

      public void sub(Mpz arg1, Mpz arg2)
      Stores arg1-arg2 into this.
    • sub

      public void sub(Mpz arg1, int arg2)
      Stores arg1-arg2 into this.
    • sub

      public void sub(int arg1, Mpz arg2)
      Stores arg1-arg2 into this.
    • sub

      public void sub(Mpz arg)
      Subtracts arg from this.
    • sub

      public void sub(int arg)
      Subtracts arg from this.
    • subRev

      public void subRev(int arg)
      Replaces this with arg-this.
    • mul

      public void mul(Mpz arg1, Mpz arg2)
      Stores arg1*arg2 into this.
    • mul

      public void mul(Mpz arg1, int arg2)
      Stores arg1*arg2 into this.
    • mul

      public void mul(Mpz arg)
      Multiplies this by arg.
    • mul

      public void mul(int arg)
      Multiplies this by arg.
    • addMul

      public void addMul(Mpz arg1, Mpz arg2)
      Stores this+arg1*arg2 into this.
    • addMul

      public void addMul(Mpz arg1, int arg2)
      Stores this+arg1*arg2 into this.
    • subMul

      public void subMul(Mpz arg1, Mpz arg2)
      Stores this-arg1*arg2 into this.
    • subMul

      public void subMul(Mpz arg1, int arg2)
      Stores this-arg1*arg2 into this.
    • mul2exp

      public void mul2exp(Mpz arg1, int arg2)
      Stores arg1*2^arg2 into this.

      arg2 must be positive.

    • mul2exp

      public void mul2exp(int arg)
      Multiplies this by 2^arg.

      arg must be positive.

    • neg

      public void neg(Mpz arg)
      Stores -arg into this.
    • neg

      public void neg()
      Replaces this with -this.
    • abs

      public void abs(Mpz arg)
      Stores |arg| into this.
    • abs

      public void abs()
      Replaces this with |this|.
    • cdivQ

      public void cdivQ(Mpz n, Mpz d) throws ArithmeticException
      Stores the quotient of n/d rounded towards +oo into this.
      Throws:
      ArithmeticException
    • cdivQ

      public void cdivQ(Mpz d) throws ArithmeticException
      Divides this by d and rounds towards +oo.
      Throws:
      ArithmeticException
    • cdivR

      public void cdivR(Mpz n, Mpz d) throws ArithmeticException
      Stores the remainder of n/d rounded towards +oo into this.
      Throws:
      ArithmeticException
    • cdivQR

      public void cdivQR(Mpz r, Mpz n, Mpz d) throws ArithmeticException
      Stores the quotient of n/d rounded towards +oo into this and the remainder into r.
      Throws:
      ArithmeticException
    • cdivQ

      public int cdivQ(Mpz n, int d) throws ArithmeticException
      Stores the quotient of n/d rounded towards +oo into this, and returns the remainder.

      d must be positive.

      Throws:
      ArithmeticException
    • cdivQ

      public int cdivQ(int d) throws ArithmeticException
      Divides this by d, rounds towards +oo, and returns the remainder.

      d must be positive.

      Throws:
      ArithmeticException
    • cdivR

      public int cdivR(Mpz n, int d) throws ArithmeticException
      Stores the remainder of n/d rounded towards +oo into this, and returns the remainder.

      d must be positive.

      Throws:
      ArithmeticException
    • cdivQR

      public int cdivQR(Mpz r, Mpz n, int d) throws ArithmeticException
      Stores the quotient of n/d rounded towards +oo into this, the remainder into r, and returns the remainder.

      d must be positive.

      Throws:
      ArithmeticException
    • cdivR

      public int cdivR(int d) throws ArithmeticException
      Returns the remainder of of this/d rounded towards +oo.

      this is unchanged.

      d must be positive.

      Throws:
      ArithmeticException
    • cdivQ2exp

      public void cdivQ2exp(Mpz n, int d) throws ArithmeticException
      Stores the quotient of n/(2^d) rounded towards +oo into this.

      d must be positive.

      Throws:
      ArithmeticException
    • cdivR2exp

      public void cdivR2exp(Mpz n, int d) throws ArithmeticException
      Stores the remainder of n/(2^d) rounded towards +oo into this.

      d must be positive.

      Throws:
      ArithmeticException
    • fdivQ

      public void fdivQ(Mpz n, Mpz d) throws ArithmeticException
      Stores the quotient of n/d rounded towards -oo into this.
      Throws:
      ArithmeticException
    • fdivQ

      public void fdivQ(Mpz d) throws ArithmeticException
      Divides this by d and rounds towards -oo.
      Throws:
      ArithmeticException
    • fdivR

      public void fdivR(Mpz n, Mpz d) throws ArithmeticException
      Stores the remainder of n/d rounded towards -oo into this.
      Throws:
      ArithmeticException
    • fdivQR

      public void fdivQR(Mpz r, Mpz n, Mpz d) throws ArithmeticException
      Stores the quotient of n/d rounded towards -oo into this and the remainder into r.
      Throws:
      ArithmeticException
    • fdivQ

      public int fdivQ(Mpz n, int d) throws ArithmeticException
      Stores the quotient of n/d rounded towards -oo into this, and returns the remainder.

      d must be positive.

      Throws:
      ArithmeticException
    • fdivQ

      public int fdivQ(int d) throws ArithmeticException
      Divides this by d, rounds towards -oo, and returns the remainder.

      d must be positive.

      Throws:
      ArithmeticException
    • fdivR

      public int fdivR(Mpz n, int d) throws ArithmeticException
      Stores the remainder of n/d rounded towards -oo into this, and returns the remainder.

      d must be positive.

      Throws:
      ArithmeticException
    • fdivQR

      public int fdivQR(Mpz r, Mpz n, int d) throws ArithmeticException
      Stores the quotient of n/d rounded towards -oo into this, the remainder into r, and returns the remainder.

      d must be positive.

      Throws:
      ArithmeticException
    • fdivR

      public int fdivR(int d) throws ArithmeticException
      Returns the remainder of of this/d rounded towards -oo.

      this is unchanged.

      d must be positive.

      Throws:
      ArithmeticException
    • fdivQ2exp

      public void fdivQ2exp(Mpz n, int d) throws ArithmeticException
      Stores the quotient of n/(2^d) rounded towards -oo into this.
      Throws:
      ArithmeticException
    • fdivR2exp

      public void fdivR2exp(Mpz n, int d) throws ArithmeticException
      Stores the remainder of n/(2^d) rounded towards -oo into this.

      d must be positive.

      Throws:
      ArithmeticException
    • tdivQ

      public void tdivQ(Mpz n, Mpz d) throws ArithmeticException
      Stores the quotient of n/d rounded towards 0 into this.
      Throws:
      ArithmeticException
    • tdivQ

      public void tdivQ(Mpz d) throws ArithmeticException
      Divides this by d and rounds towards 0.
      Throws:
      ArithmeticException
    • tdivR

      public void tdivR(Mpz n, Mpz d) throws ArithmeticException
      Stores the remainder of n/d rounded towards 0 into this.
      Throws:
      ArithmeticException
    • tdivQR

      public void tdivQR(Mpz r, Mpz n, Mpz d) throws ArithmeticException
      Stores the quotient of n/d rounded towards 0 into this and the remainder into r.
      Throws:
      ArithmeticException
    • tdivQ

      public int tdivQ(Mpz n, int d) throws ArithmeticException
      Stores the quotient of n/d rounded towards 0 into this, and returns the remainder.

      d must be positive.

      Throws:
      ArithmeticException
    • tdivQ

      public int tdivQ(int d) throws ArithmeticException
      Divides this by d, rounds towards 0, and returns the remainder.

      d must be positive.

      Throws:
      ArithmeticException
    • tdivR

      public int tdivR(Mpz n, int d) throws ArithmeticException
      Stores the remainder of n/d rounded towards 0 into this, and returns the remainder.

      d must be positive.

      Throws:
      ArithmeticException
    • tdivQR

      public int tdivQR(Mpz r, Mpz n, int d) throws ArithmeticException
      Stores the quotient of n/d rounded towards 0 into this, the remainder into r, and returns the remainder.

      d must be positive.

      Throws:
      ArithmeticException
    • tdivR

      public int tdivR(int d) throws ArithmeticException
      Returns the remainder of of this/d rounded towards 0.

      this is unchanged.

      d must be positive.

      Throws:
      ArithmeticException
    • tdivQ2exp

      public void tdivQ2exp(Mpz n, int d) throws ArithmeticException
      Stores the quotient of n/(2^d) rounded towards 0 into this.

      d must be positive.

      Throws:
      ArithmeticException
    • tdivR2exp

      public void tdivR2exp(Mpz n, int d) throws ArithmeticException
      Stores the remainder of n/(2^d) rounded towards 0 into this.

      d must be positive.

      Throws:
      ArithmeticException
    • mod

      public void mod(Mpz n, Mpz d) throws ArithmeticException
      Stores n mod |d| into this.
      Throws:
      ArithmeticException
    • mod

      public void mod(Mpz d) throws ArithmeticException
      Replaces this with this mod |d|.
      Throws:
      ArithmeticException
    • mod

      public int mod(Mpz n, int d) throws ArithmeticException
      Stores n mod |d| into this and returns it.
      Throws:
      ArithmeticException
    • mod

      public int mod(int d) throws ArithmeticException
      Replaces this with this mod |d|.
      Throws:
      ArithmeticException
    • divExact

      public void divExact(Mpz n, Mpz d) throws ArithmeticException
      Stores n/d into this, assuming that d divides n.
      Throws:
      ArithmeticException
    • divExact

      public void divExact(Mpz d) throws ArithmeticException
      Divides this by d, assuming that d divides this.
      Throws:
      ArithmeticException
    • divExact

      public void divExact(Mpz n, int d) throws ArithmeticException
      Stores n/d into this, assuming that d divides n. d must be positive.
      Throws:
      ArithmeticException
    • divExact

      public void divExact(int d) throws ArithmeticException
      Divides this by d, assuming that d divides this. d must be positive.
      Throws:
      ArithmeticException
    • isDivisible

      public boolean isDivisible(Mpz d)
      Returns true if d divides this.

      Only 0 is divisible by 0.

    • isDivisible

      public boolean isDivisible(int d)
      Returns true if d divides this.

      d must be positive.

      Only 0 is divisible by 0.

    • isDivisible2exp

      public boolean isDivisible2exp(int d)
      Returns true if 2^d divides this.

      d must be positive.

    • isCongruent

      public boolean isCongruent(Mpz c, Mpz d)
      Returns true if this equals c modulo d.

      When d=0, returns true if this equals c.

    • isCongruent

      public boolean isCongruent(int c, int d)
      Returns true if this equals c modulo d.

      d must be positive.

      When d=0, returns true if this equals c.

    • isCongruent2exp

      public boolean isCongruent2exp(Mpz c, int d)
      Returns true if this equals c modulo 2^d.

      d must be positive.

    • pow

      public void pow(Mpz b, Mpz e, Mpz m) throws ArithmeticException
      Stores b^e mod m into this.

      Throws an ArithmeticException if e < 0 and b has no inverse modulo m.

      Throws:
      ArithmeticException
    • pow

      public void pow(Mpz b, int e, Mpz m) throws ArithmeticException
      Stores b^e mod m into this.

      e must be positive.

      Throws:
      ArithmeticException
    • pow

      public void pow(Mpz b, int e)
      Stores b^e into this.

      e must be positive.

      0^0 is 1.

    • pow

      public void pow(int e)
      Takes the e-th power of this.

      e must be positive.

      0^0 is 1.

    • pow

      public void pow(int b, int e)
      Stores b^e into this.

      b and e must be positive.

      0^0 is 1.

    • root

      public boolean root(Mpz op, int n)
      Stores the n-th root of op into this, truncated.

      Returns true if the computation was exact.

      n must be positive.

    • root

      public boolean root(int n)
      Takes the n-th root of this, truncated.

      Returns true if the computation was exact.

      n must be positive.

    • root

      public void root(Mpz r, Mpz op, int n)
      Stores the n-th root of op into this, truncated, and the remainder op-this^n into r.

      n must be positive.

    • sqrt

      public void sqrt(Mpz op)
      Stores the truncated square root of op into this.
    • sqrt

      public void sqrt()
      Takes the truncated square root of this.
    • sqrt

      public void sqrt(Mpz r, Mpz op)
      Stores the square root of op into this, truncated, and the remainder op-this^2 into r.
    • isPerfectPower

      public boolean isPerfectPower()
      Returns true if this has the form a^b for some a and b > 1.

      0 and 1 are perfect powers.

    • isPerfectSquare

      public boolean isPerfectSquare()
      Returns true if this has the form a^2 for some a.

      0 and 1 are perfect squares.

    • isProbabPrime

      public int isProbabPrime(int r)
      Returns 2 if this is definitely prime, 1 if it is probably prime, and 0 if it is definitely composite.

      r is the number of tests; high numbers increasing the certainty. A value between 5 and 10 is reasonable.

    • nextPrime

      public void nextPrime(Mpz op)
      Stores into this the next prime greater than op.

      This uses a probabilistic algorithm so that the chance of getting a composite are non-null but extremely small.

    • gcd

      public void gcd(Mpz op1, Mpz op2)
      Stores into this the greatest common divisor of op1 and op2.

      The result is always positive, even for negative inputs.

    • gcd

      public int gcd(Mpz op1, int op2)
      Returns the greatest common divisor of op1 and op2.

      this is unchanged.

      op2 must be strictly positive.

    • gcd

      public void gcd(Mpz s, Mpz t, Mpz op1, Mpz op2)
      Stores the greatest common divisor of op1 and op2 into this.

      Sets s and t to values such that s*op1+t*op2 = gcd and |s| <= |op2|, |t| <= |op1|.

    • lcm

      public void lcm(Mpz op1, Mpz op2)
      Stores into this the least common multiple of op1 and op2.
    • lcm

      public void lcm(Mpz op1, int op2)
      Stores into this the least common multiple of op1 and op2.

      op2 must be positive.

    • invert

      public boolean invert(Mpz op1, Mpz op2)
      Stores the inverse of op1 modulo op2 into this.

      Returns true if this inverse exists. Otherwise, returns false; this is undefined.

    • jacobi

      public static int jacobi(Mpz b, Mpz p)
      Returns the Jacobi symbol of a and b.

      b must be odd.

    • legendre

      public static int legendre(Mpz a, Mpz p)
      Returns the Legendre symbol.

      p must be an odd positive prime.

    • kronecker

      public static int kronecker(Mpz a, Mpz b)
      Returns the Jacobi symbol with the Kronecker extension.
    • kronecker

      public static int kronecker(Mpz a, int b)
      Returns the Jacobi symbol with the Kronecker extension.
    • kronecker

      public static int kronecker(int a, Mpz b)
      Returns the Jacobi symbol with the Kronecker extension.
    • remove

      public int remove(Mpz op, Mpz f)
      Stores into this op with all occurrences of factor f removed.

      Returns the number of removed factors.

    • remove

      public int remove(Mpz f)
      Removes from this all occurrences of factor f.

      Returns the number of removed factors.

    • fac

      public void fac(int op)
      Stores op! into this.

      op must be positive.

    • bin

      public void bin(Mpz n, int k)
      Stores into this the binomial coefficient (n k).

      k must be positive.

    • bin

      public void bin(int n, int k)
      Stores into this the binomial coefficient (n k).

      n and k must be positive.

    • fib

      public void fib(int n)
      Stores into this the n-th Fibonacci number.

      n must be positive.

    • fib

      public void fib(Mpz r, int n)
      Stores into this the n-th Fibonacci number, and into r the n-1-th Fibonacci number.

      n must be positive.

    • lucnum

      public void lucnum(int n)
      Stores into this the n-th Lucas number.

      n must be positive.

    • lucnum

      public void lucnum(Mpz r, int n)
      Stores into this the n-th Lucas number, and into r the n-1-th Lucas number.

      n must be positive.

    • cmp

      public int cmp(Mpz op)
      Returns a positive, null, or negative number when this is respectively strictly greater, equal, or strictly smaller than op.
    • cmp

      public int cmp(int op)
      Returns a positive, null, or negative number when this is respectively strictly greater, equal, or strictly smaller than op.
    • cmp

      public int cmp(double op)
      Returns a positive, null, or negative number when this is respectively strictly greater, equal, or strictly smaller than op.
    • cmpAbs

      public int cmpAbs(Mpz op)
      Returns a positive, null, or negative number when |this| is respectively strictly greater, equal, or strictly smaller than |op|.
    • cmpAbs

      public int cmpAbs(int op)
      Returns a positive, null, or negative number when |this| is respectively strictly greater, equal, or strictly smaller than |op|.
    • cmpAbs

      public int cmpAbs(double op)
      Returns a positive, null, or negative number when |this| is respectively strictly greater, equal, or strictly smaller than |op|.
    • sgn

      public int sgn()
      Returns +1, 0, or -1 when this is respectively strictly positive, null, or strictly negative.
    • isEqual

      public boolean isEqual(Mpz x)
      Whether this and x have the same value.
    • equals

      public boolean equals(Object x)
      Whether x is a Mpz and has the same value as this.
      Overrides:
      equals in class Object
    • compareTo

      public int compareTo(Mpz x)
      Compare the value of this and x.
      Specified by:
      compareTo in interface Comparable<Mpz>
    • hashCode

      public int hashCode()
      Returns a hash of the value of this.
      Overrides:
      hashCode in class Object
    • isPhysicalEqual

      public boolean isPhysicalEqual(Mpz x)
      Whether this and x have the same address in memory.
    • and

      public void and(Mpz op1, Mpz op2)
      Stores the bitwise and of op1 and op2 into this.
    • and

      public void and(Mpz op)
      Stores the bitwise and of this and op into this.
    • or

      public void or(Mpz op1, Mpz op2)
      Stores the bitwise or of op1 and op2 into this.
    • or

      public void or(Mpz op)
      Stores the bitwise or of this and op into this.
    • xor

      public void xor(Mpz op1, Mpz op2)
      Stores the bitwise xor of op1 and op2 into this.
    • xor

      public void xor(Mpz op)
      Stores the bitwise xor of this and op into this.
    • not

      public void not(Mpz op)
      Stores the bitwise complement of op into this.
    • not

      public void not()
      Stores the bitwise complement of this into this.
    • popCount

      public int popCount()
      Returns the number of bits set to 1 in this.

      Returns infinite if this is negative.

    • hamDist

      public int hamDist(Mpz op)
      Returns the Hamming distance between this and op.

      Returns infinite if this and op have different signs.

    • scan0

      public int scan0(int pos)
      Returns the position of the first bit set to 0 at or after position pos.

      pos must be positive.

      May return infinite, if this is negative.

    • scan1

      public int scan1(int pos)
      Returns the position of the first bit set to 1 at or after position pos.

      pos must be positive.

      May return infinite, if this is positive.

    • setBit

      public void setBit(int pos)
      Sets the pos-th bit to 1 in this.

      pos must be positive.

    • clrBit

      public void clrBit(int pos)
      Sets the pos-th bit to 0 in this.

      pos must be positive.

    • notBit

      public void notBit(int pos)
      Inverts the pos-th bit in this.

      pos must be positive.

    • tstBit

      public boolean tstBit(int pos)
      Returns true if the pos-th bit in this is 1.

      pos must be positive.

    • fitsInt

      public boolean fitsInt()
      Whether this can fit in an int.
    • isOdd

      public boolean isOdd()
      Whether this is odd.
    • isEven

      public boolean isEven()
      Whether this is even.
    • sizeInBase

      public int sizeInBase(int b)
      Returns the number of digits necessary to represent |this| in base b.
    • size

      public int size()
      Returns the size (in GMP limbs) of the internal representation of this.
    • randomBits

      public void randomBits(RandState r, int n)
      Stores into this a uniformly distributed random number with n bits (i.e., in 0..2^n-1).

      n must be positive.

    • random

      public void random(RandState r, Mpz n)
      Stores into this a uniformly distributed random number in 0..n-1.
    • rRandomBits

      public void rRandomBits(RandState r, int n)
      Stores into this a random number with n bits, and long strings of zeros and ones in its binary representation.

      Useful to trigger corner-case bugs when testing.