Example #1
0
        public static BigInt Add(BigInt a, BigInt b)
        {
            var length = Math.Max(a.Length, b.Length);
            var result = new BigInt {Length = length};

            for (var i = 0; i < length; i++)
            {
                var temp = result.Digits[i] + a.Digits[i] + b.Digits[i];
                result.Digits[i] = temp%10;
                result.Digits[i + 1] = (temp)/10;
            }

            while (result.Digits[result.Length] > 0)
            {
                result.Length++;
            }
            return result;
        }
Example #2
0
        public static BigInt Multiply(BigInt a, BigInt b)
        {
            var result = new BigInt();

            if (a.IsZero() || b.IsZero())
            {
                return result;
            }

            for (var i = 0; i < a.Length; i++)
            {
                var p = 0;
                for (var j = 0; j < b.Length; j++)
                {
                    var temp = a.Digits[i]*b.Digits[j] + p + result.Digits[i + j];
                    result.Digits[i + j] = temp%10;
                    p = temp/10;
                }
                result.Digits[i + b.Length] = p;
            }

            result.Length = a.Length + b.Length - 1;
            while (result.Digits[result.Length] > 0)
            {
                result.Length++;
            }
            return result;
        }