Example #1
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());
        }
Example #2
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());
        }