Пример #1
0
        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;
            }
        }
Пример #2
0
        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;
            }

        }
Пример #3
0
        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;
            }

        }
Пример #4
0
 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;
 }
Пример #5
0
 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;
     }
 }
Пример #6
0
 // 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;
 }
Пример #7
0
        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;
                }
            }

        }