Beispiel #1
0
        public static BigInt Mod(BigInt a1, BigInt b) // деление по модулю
        {
            var a = new BigInt('+', new List <int>(a1.number));

            a.DeleteNuls(); b.DeleteNuls();
            if (a < b)
            {
                return(a);
            }
            else if (a == b)
            {
                return(Zero);
            }
            else
            {
                while (a > b)
                {
                    var bSDopisNuls = new BigInt(b.sign, new List <int>(b.number));

                    BigInt newraz;
                    var    razn = new BigInt((a.number.Count - b.number.Count).ToString());//1
                    bSDopisNuls *= new BigInt("10").Pow(razn);
                    if (bSDopisNuls > a)
                    {
                        newraz = razn - One;//2
                    }
                    else
                    {
                        newraz = razn;
                    }
                    var rabRazr   = new BigInt("10").Pow(newraz);//3
                    var newrazRes = b * rabRazr;
                    for (var i = 1; b *rabRazr.MultOnDigit(i) <= a; ++i)
                    {
                        var r = rabRazr.MultOnDigit(i);
                        newrazRes = r;
                    }//4
                    if (a >= newrazRes * b)
                    {
                        if (a - newrazRes * b < b)
                        {
                            a -= newrazRes * b;
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                    a -= newrazRes * b;
                }
                a.DeleteNuls();
                return(a);
            }
        }
Beispiel #2
0
 private static int CompareWithoutSign(BigInt v1, BigInt v2)
 {
     v1.DeleteNuls(); v2.DeleteNuls();
     if (v1.number.Count > v2.number.Count)
     {
         return(1);
     }
     else if (v1.number.Count < v2.number.Count)
     {
         return(-1);
     }
     else
     {
         for (var i = 0; i <= v1.number.Count - 1; i++)
         {
             if (v1.number[i] > v2.number[i])
             {
                 return(1);
             }
             else if (v1.number[i] < v2.number[i])
             {
                 return(-1);
             }
         }
         return(0);
     }
 }
Beispiel #3
0
        public static BigInt operator *(BigInt V1, BigInt V2)  // v1 * v2
        {
            V1.DeleteNuls(); V2.DeleteNuls();
            var result = new BigInt();

            result.EqualizeTheDigits(V1.number.Count + V2.number.Count);
            V1.number.Reverse(); V2.number.Reverse();
            for (var i = 0; i < V1.number.Count; ++i)
            {
                for (int j = 0, carry = 0; j < V2.number.Count || carry > 0; ++j)
                {
                    var cur = result.number[i + j] + V1.number[i] * (j < V2.number.Count ? V2.number[j] : 0) + carry;
                    result.number[i + j] = cur % 10;
                    carry = cur / 10;
                }
            }
            while (result.number.Count > 1 && result.number.Last() == 0)
            {
                result.number.RemoveAt(result.number.Count - 1);
            }
            result.sign = V1.sign == V2.sign ? '+' : '-';
            V1.number.Reverse(); V2.number.Reverse();
            result.number.Reverse();
            result.DeleteNuls();
            return(result);
        }
Beispiel #4
0
        public BigInt MultOnDigit(int n) // умножение на цифру 0, ...,9
        {
            var sign = n;

            n = Math.Abs(n);
            var result = new BigInt('+', new List <int>());
            var t = 0; int r;

            for (var i = this.number.Count - 1; i >= 0; i--)
            {
                r = this.number[i] * n + t;
                {
                    t  = r / 10;
                    r %= 10;
                }
                result.SetDigit(i, r);
            }
            if (t != 0)
            {
                result.number.Insert(0, t);
            }
            result.DeleteNuls();
            if ((this.sign == '+' && sign > 0) || (this.sign == '-' && sign < 0))
            {
                return(result);
            }
            else
            {
                return(-result);
            }
        }
Beispiel #5
0
        private static BigInt Reduce(BigInt a, BigInt b)//вычитает из большего меньшее два бигинта
        {
            if (a == b)
            {
                return(Zero);
            }
            if (b.sign == '-')
            {
                return(Addition(a, b));
            }
            a.DeleteNuls(); b.DeleteNuls();
            b.EqualizeTheDigits(a.number.Count);
            var res = new BigInt();
            var num1 = new List <int>(a.number); var num2 = new List <int>(b.number);

            for (var i = a.number.Count - 1; i >= 0; i--)
            {
                var r = num1[i] - num2[i];
                if (r < 0)
                {
                    num1[i - 1]--;
                    r = 10 + r;
                }
                res.SetDigit(i, r);
            }
            res.DeleteNuls();
            return(res);
        }
Beispiel #6
0
        public BigInt Pow(BigInt a)
        {
            this.DeleteNuls(); a.DeleteNuls();
            var res = new BigInt(sign, new List <int>(number));
            var a1  = new BigInt(a.sign, new List <int>(a.number));

            if (a1 == Zero)
            {
                return(One);
            }
            for (; a1 > One; a1 -= One)
            {
                res *= this;
            }
            return(res);
        }
Beispiel #7
0
        private static BigInt Addition(BigInt a, BigInt b)//суммирует два бигинта
        {
            var res = new BigInt();
            var maxLen = Math.Max(a.number.Count, b.number.Count) - 1;
            var enlarge = 0;
            var num1 = a.number; var num2 = b.number; var resNum = res.number;

            if (num1.Count < num2.Count)
            {
                a.EqualizeTheDigits(b.number.Count);
                res.EqualizeTheDigits(b.number.Count);
            }
            else
            {
                b.EqualizeTheDigits(a.number.Count);
                res.EqualizeTheDigits(a.number.Count);
            }
            for (var i = maxLen; i >= 0; i--)
            {
                var s = num1[i] + num2[i] + enlarge;
                if (s >= 10)
                {
                    enlarge   = 1;
                    resNum[i] = s - 10;
                }
                else
                {
                    enlarge   = 0;
                    resNum[i] = s;
                }
            }
            if (enlarge == 1)
            {
                resNum.Insert(0, 1);
            }
            res.DeleteNuls();
            return(res);
        }
Beispiel #8
0
        public static BigInt Div(BigInt a1, BigInt b) // деление нацело
        {
            var a = new BigInt('+', new List <int>(a1.number));

            a.DeleteNuls(); b.DeleteNuls();
            if (a < b)
            {
                return(Zero);
            }
            else if (a == b)
            {
                return(One);
            }
            else
            {
                var res = new List <BigInt>();
                while (a > b)
                {
                    var r           = new BigInt(b.sign, new List <int>(b.number));
                    var bSDopisNuls = new BigInt(b.sign, new List <int>(b.number));
                    var newrazRes   = Zero;
                    var newraz      = 0;
                    var razn        = a.number.Count - b.number.Count;//1
                    bSDopisNuls = bSDopisNuls.MultOn10(razn);
                    if (bSDopisNuls > a)
                    {
                        newraz = razn - 1;//2
                    }
                    else
                    {
                        newraz = razn;
                    }
                    var rabRazr = new BigInt('+' + (Math.Pow(10, newraz)).ToString());//3

                    for (var i = 1; b *rabRazr.MultOnDigit(i) < a; i++)
                    {
                        r         = rabRazr.MultOnDigit(i);
                        newrazRes = r;
                    }//4
                    if (a > newrazRes * b)
                    {
                        if (a - newrazRes * b < b)
                        {
                            res.Add(r);
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                    a -= newrazRes * b;
                    res.Add(r);
                }
                var result = Zero;
                foreach (var e in res)
                {
                    result += e;
                }
                result.sign = a.sign == b.sign ? '+' : '-';
                return(result);
            }
        }