Пример #1
0
 static void PrintAllOperations(StringBigInteger a, StringBigInteger b)
 {
     // PrintAdd(a, b);
     // PrintSubtract(a, b);
     // PrintSubtract(b, a);
     PrintMultiply(a, b);
 }
Пример #2
0
        public static StringBigInteger FullKWrapper(StringBigInteger a, StringBigInteger b)
        {
            var isNegative = a._isNegative ^ b._isNegative;
            var posA       = a._isNegative ? FlipSign(a) : a;
            var posB       = b._isNegative ? FlipSign(b) : b;
            var result     = KMultiply(a, b);

            return(isNegative ? FlipSign(result) : result);
        }
Пример #3
0
        private static StringBigInteger MultiplyByTens(StringBigInteger ac, int n)
        {
            if (ac._value == "0")
            {
                return(ac);
            }
            var value = ac._value.PadRight(n + ac.Length, '0');

            return(new StringBigInteger(value));
        }
Пример #4
0
        private static StringBigInteger Bottom(StringBigInteger x, int n_2, int n)
        {
            var m = n_2 - (n - x.Length);

            if (m == 0)
            {
                return(x);
            }
            if (m >= x.Length)
            {
                return(new StringBigInteger("0"));
            }
            return(new StringBigInteger(x._value.Substring(m)));
        }
Пример #5
0
        public static StringBigInteger KMultiply(StringBigInteger x, StringBigInteger y)
        {
            if (x._value == "0" || y._value == "0")
            {
                return(new StringBigInteger("0"));
            }
            if (x._value == "1")
            {
                return(y);
            }
            if (y._value == "1")
            {
                return(x);
            }
            if (x.Length == 1 && y.Length == 1)
            {
                return(SingleDigitMultiply(x, y));
            }
            var compare = CompareIgnoreSign(x, y);
            var bigger  = compare >= 0 ? x : y;
            var smaller = compare >= 0 ? y : x;
            var n       = bigger.Length;
            var n_2     = n / 2;
            var a       = Top(bigger, n_2, n);
            var b       = Bottom(bigger, n_2, n);
            var c       = Top(smaller, n_2, n);
            var d       = Bottom(smaller, n_2, n);

            var ac = KMultiply(a, c);
            var bd = KMultiply(b, d);
            var ad = KMultiply(a, d);
            var bc = KMultiply(b, c);
            //note - this is not really doing karatsuba, because it's not doing the proper calculation that avoids 4 recursive calls
            //solving that problem requires modifying the algorithm to correctly handle multiplication of two numbers
            //with different numbers of digits
            var ad_bc = ad + bc; // KMultiply(a + b, c + d) - ac - bd;

            var ac_n      = MultiplyByTens(ac, n);
            var ad_bc_n_2 = MultiplyByTens(ad_bc, n_2);
            var result    = ac_n + ad_bc_n_2 + bd;

            return(result);
        }
Пример #6
0
 private static int CompareIgnoreSign(StringBigInteger a, StringBigInteger b)
 {
     if (a._value.Length > b._value.Length)
     {
         return(1);
     }
     if (a._value.Length < b._value.Length)
     {
         return(-1);
     }
     for (int i = 0; i < a._value.Length; i++)
     {
         var compare = a._value[i] - b._value[i];
         if (compare != 0)
         {
             return(compare);
         }
     }
     return(0);
 }
Пример #7
0
        static void Main(string[] args)
        {
            var c = new StringBigInteger("100");
            var d = new StringBigInteger("100");

            PrintAllOperations(c, d);

            var a = new StringBigInteger("54");
            var b = new StringBigInteger("12");

            PrintAllOperations(a, b);

            var a0 = new StringBigInteger("5454");
            var b0 = new StringBigInteger("1212");

            PrintAllOperations(a0, b0);

            // var a1 = new StringBigInteger("321");
            // var b1 = new StringBigInteger("345");
            // PrintAllOperations(a1, b1);

            // var a2 = new StringBigInteger("54321");
            // var b2 = new StringBigInteger("12345");
            // PrintAllOperations(a2, b2);

            // var a3 = new StringBigInteger("1234");
            // var b3 = new StringBigInteger("321");
            // PrintAllOperations(a3, b3);

            // var a4 = new StringBigInteger("1234");
            // var b4 = new StringBigInteger("123");
            // PrintAllOperations(a4, b4);

            // var a5 = new StringBigInteger("3141592653589793238462643383279502884197169399375105820974944592");
            // var b5 = new StringBigInteger("2718281828459045235360287471352662497757247093699959574966967627");
            // PrintAllOperations(a5, b5);
        }
Пример #8
0
        private static StringBigInteger Add(StringBigInteger a, StringBigInteger b)
        {
            bool sameSign   = a._isNegative == b._isNegative;
            var  compare    = CompareIgnoreSign(a, b);
            var  aIsSmaller = compare < 0;
            var  bigger     = aIsSmaller ? b : a;
            var  smaller    = aIsSmaller ? a : b;

            if (sameSign)
            {
                var addResult = AddStrings(bigger._value, smaller._value);
                return(new StringBigInteger(addResult, a._isNegative));
            }
            else if (compare == 0 && b._isNegative)
            {
                return(new StringBigInteger("0"));
            }
            else
            {
                var    isNegative = aIsSmaller == b._isNegative;
                string addResult  = AddStrings(bigger._value, smaller._value, true);
                return(new StringBigInteger(addResult, isNegative));
            }
        }
Пример #9
0
 private static StringBigInteger SingleDigitMultiply(StringBigInteger x, StringBigInteger y)
 => new StringBigInteger(((x._value[0] - '0') * (y._value[0] - '0')).ToString());
Пример #10
0
 public static StringBigInteger FlipSign(StringBigInteger value) => new StringBigInteger(value._value, !value._isNegative);
Пример #11
0
 private static void PrintMultiply(StringBigInteger a, StringBigInteger b)
 {
     Console.WriteLine($"{a} * {b} = {a * b}");
 }
Пример #12
0
 static void PrintSubtract(StringBigInteger a, StringBigInteger b)
 {
     Console.WriteLine($"{a} - {b} = {a - b}");
 }
Пример #13
0
 static void PrintAdd(StringBigInteger a, StringBigInteger b)
 {
     Console.WriteLine($"{a} + {b} = {a + b}");
 }