Exemplo n.º 1
0
        private static Integer Mod(Integer a, Integer 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.RemoveNullTypes();

            result.Sign = a.Sign == b.Sign ? Sign.Plus : Sign.Minus;
            return(result);
        }
Exemplo n.º 2
0
        private static Integer Add(Integer a, Integer b)
        {
            var numbers   = 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);
                if (sum >= 10)
                {
                    sum   -= 10;
                    reduce = 1;
                }
                else
                {
                    reduce = 0;
                }

                numbers.Add(sum);
            }

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

            return(new Integer(a.Sign, numbers));
        }
Exemplo n.º 3
0
        private static int Comparenumbers(Integer a, Integer 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);
                }
                else if (a.GetByte(i) > b.GetByte(i))
                {
                    return(1);
                }
            }
            return(0);
        }
Exemplo n.º 4
0
        private static Integer Multiply(Integer a, Integer 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);
        }
Exemplo n.º 5
0
        private static Integer Subtract(Integer a, Integer b)
        {
            var numbers = 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;
                }

                numbers.Add((byte)sub);
            }

            return(new Integer(max.Sign, numbers));
        }
Exemplo n.º 6
0
        private static Integer Add(Integer a, Integer 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 Integer(a.Sign, digits));
        }
Exemplo n.º 7
0
        private static Integer Div(Integer a, Integer 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.RemoveNullTypes();
            result.Sign = a.Sign == b.Sign ? Sign.Plus : Sign.Minus;
            return(result);
        }