Esempio n. 1
0
        public static bool operator >(HugeInteger a, HugeInteger b)
        {
            if (a.sign < b.sign)
            {
                return(false);
            }
            else if (a.sign > b.sign)
            {
                return(true);
            }
            int comp = HugeInteger.CompareUnsigned(a, b);

            return(comp == a.sign);
        }
Esempio n. 2
0
        public static HugeInteger operator +(HugeInteger initial, HugeInteger added)
        {
            int         compare = HugeInteger.CompareUnsigned(initial, added);
            HugeInteger max, min;

            (max, min) = (compare == -1) ? (added, initial) : (initial, added);

            int[] initialNumber = max.number;
            bool  swap          = false;

            int method = (max.sign == min.sign) ? 1 : -1;

            if (method == -1 && max.sign == -1)
            {
                max.sign = -max.sign; min.sign = -min.sign; swap = true;
            }

            try
            {
                int i = initial.maxLength - 1;
                while (i >= max.maxLength - max.Length)
                {
                    int sum = (method == 1) ? initialNumber[i] + min.number[i] : max.sign * initialNumber[i] + min.sign * min.number[i];
                    initialNumber[i] = (sum < 0) ? (10 + sum) : (sum % 10);
                    if (sum >= 10 || sum < 0)
                    {
                        initialNumber[i - 1] += (sum < 0) ? -1 : ((sum >= 10) ? 1 : 0);
                    }
                    i--;
                }

                if (swap)
                {
                    max.sign = -max.sign; min.sign = -min.sign;
                }
            }
            catch (IndexOutOfRangeException) { throw new OverflowException(); }

            return(new HugeInteger(initialNumber, max.sign));
        }
Esempio n. 3
0
 public static bool operator >=(HugeInteger a, HugeInteger b)
 {
     return(a > b || HugeInteger.CompareUnsigned(a, b) == 0 && a.sign == b.sign);
 }
Esempio n. 4
0
        private static (string, string) Divide(HugeInteger initial, HugeInteger divided)
        {
            int sign = initial.sign * divided.sign; initial.sign = 1; divided.sign = 1;

            if (initial < divided)
            {
                return("0", initial.ToString());
            }
            if (divided.isZero())
            {
                throw new DivideByZeroException();
            }
            if (divided.ToString() == "1")
            {
                return(((sign == -1) ? "-" : "") + initial.ToString(), "0");
            }

            int zeros = Math.Min(divided.Length - divided.ToString().TrimEnd('0').Length, initial.Length - initial.ToString().TrimEnd('0').Length);

            divided = new HugeInteger(divided.ToString().Substring(0, divided.Length - zeros));
            initial = new HugeInteger(initial.ToString().Substring(0, initial.Length - zeros));

            HugeInteger newNumber;
            string      dividedNumberStr = divided.ToString();

            string q, tmp, sliceI, tail; q = tmp = tail = sliceI = "";
            bool   first = true;
            int    i, count;
            int    lastLength = divided.Length;


            while (initial >= divided)
            {
                i      = initial.maxLength - initial.Length;
                sliceI = String.Join("", initial.number.Skip(i).Take(lastLength + ((first) ? 0 : 1)));
                count  = (first) ? 1 : 2;

                while (HugeInteger.CompareUnsigned(sliceI, dividedNumberStr) < 0)
                {
                    if (!first)
                    {
                        q += "0";
                    }
                    sliceI = String.Join("", initial.number.Skip(i).Take(lastLength + count));
                    count++;
                }

                first = false;

                (newNumber, tmp) = HugeInteger.DivideBySub(new HugeInteger(sliceI), new HugeInteger(dividedNumberStr));
                q         += tmp;
                lastLength = newNumber.Length;
                tail       = String.Join("", initial.number.Skip(initial.maxLength - initial.Length + sliceI.Length).Take(initial.maxLength - (initial.maxLength - initial.Length + sliceI.Length)));
                initial    = new HugeInteger(newNumber.ToString() + tail);
            }

            if (tail != "")
            {
                q = q.PadRight(q.Length + tail.Length, '0');
            }

            return(((sign == -1) ? "-" : "") + q, initial.ToString());
        }
Esempio n. 5
0
        private static (string, string) Divide(HugeInteger initial, HugeInteger divided)
        {
            int sign = initial.sign * divided.sign; initial.sign = 1; divided.sign = 1;

            if (initial < divided)
            {
                return("0", initial.ToString());
            }
            if (divided.isZero())
            {
                throw new DivideByZeroException();
            }
            if (divided.ToString() == "1")
            {
                return(((sign == -1) ? "-" : "") + initial.ToString(), "0");
            }

            int zeros = Math.Min(divided.Length - String.Join("", divided.number).TrimStart('0').Length, initial.Length - String.Join("", initial.number).TrimStart('0').Length);

            string dividedNumberStr = String.Join("", divided.number.Skip(zeros).Take(divided.Length - zeros).Reverse()).TrimStart('0');
            string initialNumberStr = String.Join("", initial.number.Skip(zeros).Take(initial.Length - zeros).Reverse()).TrimStart('0');

            HugeInteger newNumber;

            string q, tmp, tail; q = tmp = tail = "";
            bool   first = true;
            bool   zero  = true;
            string sliceI;
            int    count;
            int    lastLength = divided.Length;

            while (HugeInteger.CompareUnsigned(initialNumberStr, dividedNumberStr) >= 0)
            {
                count = (zero) ? 0 : 1;
                int sliceLength = lastLength + count;
                sliceI = String.Join("", initialNumberStr.Take(sliceLength));
                count++;


                while (HugeInteger.CompareUnsigned(sliceI, dividedNumberStr) < 0)
                {
                    if (!first)
                    {
                        q += "0";
                    }
                    sliceI += initialNumberStr[sliceI.Length].ToString();
                    count++;
                }

                first = false;

                (newNumber, tmp) = HugeInteger.DivideBySub(new HugeInteger(sliceI), divided);

                zero = newNumber.isZero() ? true : false;

                q         += tmp;
                lastLength = newNumber.Length;
                tail       = String.Join("", initialNumberStr.Skip(sliceI.Length).Take(initial.Length - sliceI.Length));

                initialNumberStr = ((!newNumber.isZero()) ? newNumber.ToString() : "") + tail;
            }

            if (tail != "")
            {
                q = q.PadRight(q.Length + tail.Length, '0');
            }


            return(((sign == -1) ? "-" : "") + q, initial.ToString());
        }