Beispiel #1
0
        public static BigNum operator/(BigNum A, BigNum B)
        {
            BigNum L1 = new BigNum(1),
                   t1 = new BigNum(A),
                   t2 = new BigNum(B),
                   t  = new BigNum();
            bool an   = A.Is_Negative(),
                 bn   = B.Is_Negative();

            if (an)
            {
                t1.Neg();
            }
            if (bn)
            {
                t2.Neg();
            }
            int l1 = t1.Last_Bit_Pos(), l2 = t2.Last_Bit_Pos();

            while (l1 > l2)
            {
                int p = l1 - l2 - 1;
                for (int i = 0; i < p; i++)
                {
                    t2.Shift_Left_Once();
                    L1.Shift_Left_Once();
                }
                t  = t + L1;
                t1 = t1 - t2;
                for (int i = 0; i < p; i++)
                {
                    t2.Shift_Right_Once();
                    L1.Shift_Right_Once();
                }
                l1 = t1.Last_Bit_Pos();
                l2 = t2.Last_Bit_Pos();
            }
            t1 = t1 - t2;
            if (!t1.Is_Negative())
            {
                t = t + L1;
            }
            if (an)
            {
                t.Neg();
            }
            if (bn)
            {
                t.Neg();
            }
            return(t);
        }
Beispiel #2
0
        public void Print()
        {
            bool   neg = false;
            BigNum T   = new BigNum(this);

            if (T.Is_Negative())
            {
                neg = true;
            }
            if (neg)
            {
                T.Neg();
            }
            List <int> t1 = new List <int>(),
                       t2 = new List <int>();

            t1.Add(1);
            int carry = 0;

            while (T.Greater_Than_Zero())
            {
                if ((T.BigNum_ary[0] & 1) == 1)
                {
                    for (int i = 0; i < t2.Count; i++)
                    {
                        t2[i] += t1[i];
                    }
                    for (int i = t2.Count; i < t1.Count; i++)
                    {
                        t2.Add(t1[i]);
                    }
                    for (int i = 0; i < t2.Count; i++)
                    {
                        t2[i] += carry;
                        carry  = 0;
                        carry  = (t2[i] - (t2[i] % 10)) / 10;
                        t2[i] %= 10;
                    }
                    while (carry > 0)
                    {
                        t2.Add(carry % 10);
                        carry /= 10;
                    }
                }
                for (int i = 0; i < t1.Count; i++)
                {
                    t1[i] *= 2;
                    t1[i] += carry;
                    carry  = 0;
                    carry  = (t1[i] - (t1[i] % 10)) / 10;
                    t1[i] %= 10;
                }
                while (carry > 0)
                {
                    t1.Add(carry % 10);
                    carry /= 10;
                }
                T.Shift_Right_Once();
            }
            t2.Reverse();
            if (neg)
            {
                Console.Write("-");
            }
            t2.ForEach(i => Console.Write("{0}", i));
            Console.WriteLine();
        }