Example #1
0
            // ---------------------------------------------------------------------

            private static void MultiplyKaratsuba(int BASE, IList <int> result, IList <int> one, IList <int> two, int dim)
            {
                int half = dim / 2;

                // Отбрасываем при некотором значении
                if (dim <= karatsubaCutoffDimension)
                {
                    LongIntegerMethods.MultiplySimple(BASE, result, one, two);
                    return;
                }

                ListSegment <int> a = new ListSegment <int>(one, 0, half);
                ListSegment <int> b = new ListSegment <int>(one, half, one.Count - half);

                ListSegment <int> c = new ListSegment <int>(two, 0, half);
                ListSegment <int> d = new ListSegment <int>(two, half, two.Count - half);

                int[] ac   = new int[dim];
                int[] bd   = new int[b.Count + d.Count];
                int[] abcd = new int[b.Count + d.Count + 2];

                MultiplyKaratsuba(BASE, ac, a, c, half);
                MultiplyKaratsuba(BASE, bd, b, d, half);

                int[] apb   = new int[b.Count + 1];
                int[] cpd   = new int[d.Count + 1];
                int[] acpbd = new int[b.Count + d.Count + 2];

                LongIntegerMethods.Sum(BASE, apb, a, b);
                LongIntegerMethods.Sum(BASE, cpd, c, d);

                MultiplyKaratsuba(BASE, abcd, apb, cpd, half);

                LongIntegerMethods.Sum(BASE, acpbd, ac, bd);

                int[] difference = new int[b.Count + d.Count + 2];

                if (LongIntegerMethods.Dif(BASE, difference, abcd, acpbd))
                {
                    Console.WriteLine("Lower-level difference error.");
                }

                SumPrivate(BASE, result, ac, 0, difference, half);
                SumPrivate(BASE, result, result, 0, bd, dim);

                return;
            }
Example #2
0
            //---------------------------------------------------------------
            //---------------------NUMBER MULTIPLICATION---------------------
            //---------------------------------------------------------------

            /// <summary>
            /// Performs a simple, square-complex multiplication of two LongInt numbers.
            /// </summary>
            /// <param name="one"></param>
            /// <param name="two"></param>
            /// <returns></returns>
            public static LongInt <B> MultiplySimple(LongInt <B> one, LongInt <B> two)
            {
                // the resulting number can have double length.

                LongInt <B> res = new LongInt <B>(one.Length + two.Length);

                res.Digits.AddRange(new int[one.Length + two.Length]);
                res.Negative = one.Negative ^ two.Negative;

                // The big one

                LongIntegerMethods.MultiplySimple(LongInt <B> .BASE, res.Digits, one.Digits, two.Digits);

                // Cut the leading zeroes

                res.DealWithZeroes();
                return(res);
            }