Beispiel #1
0
        private static MyBigInteger Mod(MyBigInteger a, MyBigInteger b)
        {
            var result = Zero;

            for (var i = a.Size - 1; i >= 0; i--)
            {
                result += Exp(a.GetByte(i), i);

                var x     = 0;
                var left  = 0;
                var right = 10;

                while (left <= right)
                {
                    var middle  = (left + right) >> 1;
                    var current = b * Exp((byte)middle, i);
                    if (current <= result)
                    {
                        x    = middle;
                        left = middle + 1;
                    }
                    else
                    {
                        right = middle - 1;
                    }
                }

                result -= b * Exp((byte)x, i);
            }

            result.RemoveNulls();

            result.Sign = a.Sign == b.Sign ? Sign.Plus : Sign.Minus;
            return(result);
        }
Beispiel #2
0
        private static int CompareDigits(MyBigInteger a, MyBigInteger b)
        {
            var maxLength = Math.Max(a.Size, b.Size);

            for (var i = maxLength; i >= 0; i--)
            {
                if (a.GetByte(i) < b.GetByte(i))
                {
                    return(-1);
                }
                if (a.GetByte(i) > b.GetByte(i))
                {
                    return(1);
                }
            }
            return(0);
        }
Beispiel #3
0
        private static MyBigInteger Multiply(MyBigInteger a, MyBigInteger b)
        {
            var result = Zero;

            for (var i = 0; i < a.Size; i++)
            {
                for (int j = 0, reduce = 0; (j < b.Size) || (reduce > 0); j++)
                {
                    var sum = result.GetByte(i + j) + a.GetByte(i) * b.GetByte(j) + reduce;
                    result.SetByte(i + j, (byte)(sum % 10));
                    reduce = sum / 10;
                }
            }

            result.Sign = a.Sign == b.Sign ? Sign.Plus : Sign.Minus;
            return(result);
        }
Beispiel #4
0
        private static MyBigInteger Subtract(MyBigInteger a, MyBigInteger b)
        {
            var digits  = new List <byte>();
            var max     = Zero;
            var min     = Zero;
            var compare = Comparison(a, b, ignoreSign: true);

            switch (compare)
            {
            case -1:
                min = a;
                max = b;
                break;

            case 0:
                return(Zero);

            case 1:
                min = b;
                max = a;
                break;
            }

            var maxLength = Math.Max(a.Size, b.Size);

            var reduce = 0;

            for (var i = 0; i < maxLength; i++)
            {
                var sub = max.GetByte(i) - min.GetByte(i) - reduce;
                if (sub < 0)
                {
                    sub   += 10;
                    reduce = 1;
                }
                else
                {
                    reduce = 0;
                }

                digits.Add((byte)sub);
            }

            return(new MyBigInteger(max.Sign, digits));
        }
Beispiel #5
0
        private static MyBigInteger Add(MyBigInteger a, MyBigInteger b)
        {
            var digits    = new List <byte>();
            var maxLength = Math.Max(a.Size, b.Size);
            var reduce    = 0;

            for (var i = 0; i < maxLength; i++)
            {
                var sum = (byte)(a.GetByte(i) + b.GetByte(i) + reduce);
                reduce = sum / 10;
                digits.Add((byte)(sum % 10));
            }

            if (reduce > 0)
            {
                digits.Add((byte)reduce);
            }

            return(new MyBigInteger(a.Sign, digits));
        }
Beispiel #6
0
        private static MyBigInteger Div(MyBigInteger a, MyBigInteger b)
        {
            var result  = Zero;
            var current = Zero;

            for (var i = a.Size - 1; i >= 0; i--)
            {
                current += Exp(a.GetByte(i), i);

                var x     = 0;
                var left  = 0;
                var right = 10;
                while (left <= right)
                {
                    var middle = (left + right) / 2;
                    var cur    = b * Exp((byte)middle, i);
                    if (cur <= current)
                    {
                        x    = middle;
                        left = middle + 1;
                    }
                    else
                    {
                        right = middle - 1;
                    }
                }

                result.SetByte(i, (byte)(x % 10));
                var reduce = b * Exp((byte)x, i);
                current = current - reduce;
            }

            result.RemoveNulls();
            result.Sign = a.Sign == b.Sign ? Sign.Plus : Sign.Minus;
            return(result);
        }