public mpz_t Divide(int x, out int remainder) { mpz_t quotient = new mpz_t(); if(x >= 0) { remainder = (int)mpir.mpz_tdiv_q_ui(quotient, this, (uint)x); return quotient; } else { remainder = -(int)mpir.mpz_tdiv_q_ui(quotient, this, (uint)(-x)); mpz_t res = -quotient; quotient.Dispose(); return res; } }
public mpz_t DivideExactly(int x) { mpz_t z = new mpz_t(); mpir.mpz_divexact_ui(z, this, (uint)x); if (x < 0) { mpz_t res = -z; z.Dispose(); return res; } else { return z; } }
public static mpz_t operator /(mpz_t x, int y) { if (y >= 0) { mpz_t quotient = new mpz_t(); mpir.mpz_tdiv_q_ui(quotient, x, (uint)y); return quotient; } else { mpz_t quotient = new mpz_t(); mpir.mpz_tdiv_q_ui(quotient, x, (uint)(-y)); mpz_t negQ = -quotient; quotient.Dispose(); return negQ; } }
public static mpz_t operator --(mpz_t x) { mpz_t z = new mpz_t(); mpir.mpz_sub_ui(z, x, 1); mpir.mpz_set(x, z); z.Dispose(); return x; }
public static mpz_t operator -(int x, mpz_t y) { if(x >= 0) { mpz_t z = new mpz_t(); mpir.mpz_ui_sub(z, (uint)x, y); return z; } else { mpz_t z = new mpz_t(); mpir.mpz_add_ui(z, y, (uint)(-x)); mpz_t z1 = -z; z.Dispose(); return z1; } }
// TODO: Optimize by accessing the memory directly public int CompareTo(ulong other) { mpz_t otherMpz = new mpz_t(other); int ret = this.CompareTo(otherMpz); otherMpz.Dispose(); return ret; }
public static mpz_t Binomial(int n, int k) { if(k < 0) throw new ArgumentOutOfRangeException(); mpz_t z = new mpz_t(); if(n >= 0) { mpir.mpz_bin_uiui(z, (uint)n, (uint)k); return z; } else { // Use the identity bin(n,k) = (-1)^k * bin(-n+k-1,k) mpir.mpz_bin_uiui(z, (uint)(-n + k - 1), (uint)k); if ((k & 1) != 0) { mpz_t res = -z; z.Dispose(); return res; } else { return z; } } }