Example #1
0
        //IComparable nonstatic implementation
        public int CompareTo(object obj)
        {
            LongIntBin snd = (LongIntBin)obj;

            if (Bits.Length > snd.Bits.Length)
            {
                return(1);
            }
            else if (Bits.Length < snd.Bits.Length)
            {
                return(-1);
            }
            else
            {
                int maxlenght = Math.Max(Bits.Length, snd.Bits.Length);
                for (int i = maxlenght - 1; i >= 0; i--)
                {
                    int aval = i >= Bits.Length ? 0 : (Bits[i] ? 1 : 0);
                    int bval = i >= snd.Bits.Length ? 0 : (snd.Bits[i] ? 1 : 0);
                    if (aval > bval)
                    {
                        return(1);
                    }
                    else if (aval < bval)
                    {
                        return(-1);
                    }
                }
                return(0);
            }
        }
Example #2
0
        public static LongIntBin operator -(LongIntBin a, LongIntBin b)
        {
            int        l   = a.Bits.Length;
            int        r   = b.Bits.Length;
            LongIntBin res = new LongIntBin();
            //Lenght of the result BitArray
            int n = Math.Max(l, r) + 1;

            res.Bits = new BitArray(n);
            bool carry = false;

            for (int i = 0; i < n; i++)
            {
                //Automatical 0-filling and out-of-range prevention
                int aval = (i < l ? (a.Bits[i] ? 1 : 0) : 0);
                int bval = (i < r ? (b.Bits[i] ? 1 : 0) : 0);
                int cval = (carry ? 1 : 0);

                int value = aval - bval - cval;
                switch (value)
                {
                case 1: res.Bits[i] = true; carry = false; break;

                case 0: res.Bits[i] = false; carry = false; break;

                case -1: res.Bits[i] = true; carry = true; break;

                case -2: res.Bits[i] = false; carry = true; break;
                }
            }
            return(res);
        }
Example #3
0
        static void PerformanceTest(int n)
        {
            WriteLine($"---------   n = {n}");
            DateTime start   = DateTime.Now;
            DateTime current = DateTime.Now;

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    LongIntBin a = i;
                    LongIntBin b = j;
                    LongIntBin c = a + b;
                }
            }
            Console.WriteLine($"Add time {DateTime.Now - current}");
            current = DateTime.Now;
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    LongIntBin a = i;
                    LongIntBin b = j;
                    LongIntBin c = a * b;
                }
            }
            Console.WriteLine($"Mult time {DateTime.Now - current}");
            current = DateTime.Now;
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    LongIntBin a = i;
                    LongIntBin b = j;
                    LongIntBin c = a - b;
                }
            }
            Console.WriteLine($"Substr time {DateTime.Now - current}");
            current = DateTime.Now;
            WriteLine($"Total {current - start}");
        }
Example #4
0
        //Naive Multiplication alg - O(n^2)
        private static LongIntBin NaiveMult(LongIntBin a, LongIntBin b)
        {
            int        l   = a.Bits.Length;
            int        r   = b.Bits.Length;
            LongIntBin res = new LongIntBin();

            res.Bits = new BitArray(l + r);
            bool carry = false;

            for (int i = 0; i < l; i++)
            {
                carry = false;
                for (int j = 0; j < r; j++)
                {
                    bool oldval = res.Bits[i + j];
                    bool result = a.Bits[i] & b.Bits[j];
                    res.Bits[i + j] ^= carry ^ result;
                    carry            = (oldval | carry) & (oldval | result) &
                                       (carry | result);
                }
                res.Bits[i + r] ^= carry;
            }
            return(res);
        }
Example #5
0
        static void StaticManualTestBin(LongIntBin n1, LongIntBin n2)
        {
            WriteLine("----------");
            WriteLine($"n1 = {n1}, n2 = {n2}");
            WriteLine($"{n1} + {n2} = {n1 + n2}");
            WriteLine($"{n1} - {n2} = {n1 - n2}");
            WriteLine($"{n1} * {n2} = {n1 * n2}");
            WriteLine($"{n1} / {n2} = {n1 / n2}");
            WriteLine($"{n1} % {n2} = {n1 % n2}");
            WriteLine($"{n1} > {n2} = {n1 > n2}");
            WriteLine($"{n1} < {n2} = {n1 < n2}");
            WriteLine($"{n1} >= {n2} = {n1 >= n2}");
            WriteLine($"{n1} <= {n2} = {n1 <= n2}");
            WriteLine($"{n1} == {n2} = {n1 == n2}");
            WriteLine($"{n1} != {n2} = {n1 != n2}");

            LongIntBin num = 1;

            for (int i = 0; i < 200; i++)
            {
                num *= int.MaxValue;
            }
            WriteLine($"Big Number {num}");
        }
Example #6
0
 //O(n^log(3) ~ n^1.58)
 private static LongIntBin KaratsubaMult(LongIntBin a, LongIntBin b)
 {
     return(0);
 }