Esempio n. 1
0
        /// <summary>第一次用开方尝试</summary>
        private static List <int> FirstTry(List <int> result, List <int> div)
        {
            int tryDiv = BigNumber.Max / 2;
            int low    = 1;
            int top    = BigNumber.Max - 1;

            //第一位数是1

            //第一用平方试商
            while (true)
            {
                if (BigCalculate.CompareList(new List <int>()
                {
                    1
                }, div) == 0)
                {
                    div = BigCalculate.IntMinus(div, new List <int>()
                    {
                        1
                    });
                    result.Add(1);
                    break;
                }
                //连9都小了,那么就是9
                if (BigCalculate.CompareList(BigCalculate.Multiply(BigNumber.Max - 1, BigNumber.Max - 1), div) == -1)
                {
                    div = BigCalculate.IntMinus(div, BigCalculate.Multiply(BigNumber.Max - 1, BigNumber.Max - 1));
                    result.Add(BigNumber.Max - 1);
                    break;
                }
                int c = BigCalculate.CompareList(BigCalculate.Multiply(tryDiv, tryDiv), div);
                //商大了
                if (c == -1)
                {
                    low    = tryDiv;
                    tryDiv = (low + top) / 2;
                }
                else if (c == 1)     //商小了
                {
                    top    = tryDiv;
                    tryDiv = (low + top) / 2;
                }
                else     //刚好相等
                {
                    div.Clear();
                    result.Add(tryDiv);
                    break;
                }

                if (low + 1 == top)
                {
                    div = BigCalculate.IntMinus(div, BigCalculate.Multiply(low, low));
                    result.Add(low);
                    break;
                }
            }
            return(div);
        }
Esempio n. 2
0
        /// <summary>执行一次试商的运算</summary>
        /// <param name="div">已经加组的被除数</param>
        /// <param name="result">已生成的结果</param>
        static List <int> TryDiv(List <int> div, List <int> result)
        {
            int i   = BigNumber.Max / 2;
            int low = 0;
            int top = BigNumber.Max - 1;

            //连1都大了,那么商为0,除数不变
            if (CompartDiv(1, result, div) == 1)
            {
                result.Add(0);
                return(div);
            }
            //连9999都小了
            if (CompartDiv(BigNumber.Max - 1, result, div) == -1)
            {
                List <int> r = BigCalculate.IntMinus(div, CalDiv(BigNumber.Max - 1, result));
                result.Add(BigNumber.Max - 1);
                return(r);
            }

            while (true)
            {
                int c = CompartDiv(i, result, div);
                //商大了
                if (c == 1)
                {
                    top = i;
                    i   = (low + top) / 2;
                }
                //商小了
                else if (c == -1)
                {
                    low = i;
                    i   = (low + top) / 2;
                }
                //刚好相等
                else
                {
                    //div.Clear();
                    result.Add(i);
                    return(new List <int>());
                }
                //已找到合适的商
                if (low + 1 == top)
                {
                    List <int> r = BigCalculate.IntMinus(div, CalDiv(low, result));
                    result.Add(low);
                    return(r);
                }
            }
        }