Beispiel #1
0
        public MyInt max(MyInt b)
        {
            byte[] valueA = getValue();
            byte[] valueB = b.getValue();

            bool isPositiveA = valueA[0] == 0;
            bool isPositiveB = valueB[0] == 0;

            if (isPositiveA != isPositiveB)
            {
                return(isPositiveA ? this : b);
            }

            int lengthA = valueA.Length;
            int lengthB = valueB.Length;

            if (lengthA != lengthB)
            {
                return(lengthA > lengthB ? this : b);
            }

            int numA = 0, numB = 0, i = 1;

            do
            {
                numA = valueA[i];
                numB = valueB[i];
                i++;
            } while (numA == numB && i < lengthA);

            return(numA > numB ? this : b);
        }
Beispiel #2
0
        public MyInt multiply(MyInt b)
        {
            byte[] valueA = getValue();
            byte[] valueB = b.getValue();

            byte[] result = new byte[Math.Max(valueA.Length, valueB.Length) * 2];

            bool isPositiveA = valueA[0] == 0;
            bool isPositiveB = valueB[0] == 0;

            valueA = valueA.Skip(1).Reverse().ToArray();
            valueB = valueB.Skip(1).Reverse().ToArray();

            for (int i = 0; i < valueA.Length; i++)
            {
                for (int j = 0, carry = 0; j < valueB.Length || carry > 0; j++)
                {
                    int sum = result[i + j] + valueA[i] * (valueB.Length - 1 < j ? 0 : valueB[j]) + carry;
                    result[i + j] = (byte)(sum % 10);
                    carry         = sum / 10;
                }
            }

            result[result.Length - 1] = (byte)(isPositiveA == isPositiveB ? 0 : 1);

            return(new MyInt(result.Reverse().ToArray()));
        }
Beispiel #3
0
        public void testAdd()
        {
            MyInt a = new MyInt(549);
            MyInt b = new MyInt(475);

            Assert.AreEqual(a.add(b).longValue(), 1024);

            MyInt c = new MyInt("10000003454");

            Assert.AreEqual(b.add(c).ToString(), "10000003929");

            MyInt d = new MyInt(-500);

            Assert.AreEqual(d.add(a).longValue(), 49);

            Assert.AreEqual(d.add(b).longValue(), -25);

            Assert.AreEqual((new MyInt(0)).add(new MyInt(-0)).longValue(), 0);

            MyInt e = new MyInt(-3554);

            Assert.AreEqual(c.add(e).ToString(), "9999999900");

            Assert.AreEqual(e.add(d).longValue(), -4054);
        }
Beispiel #4
0
        public void testMultiply()
        {
            MyInt a = new MyInt(456);
            MyInt b = new MyInt(4356);
            MyInt c = new MyInt(-5493);
            MyInt d = new MyInt(-3205);

            Assert.AreEqual(a.multiply(b).longValue(), 1986336);
            Assert.AreEqual(b.multiply(c).longValue(), -23927508);
            Assert.AreEqual(c.multiply(d).longValue(), 17605065);
        }
Beispiel #5
0
        public void testSubstract()
        {
            MyInt a = new MyInt(678);
            MyInt b = new MyInt(1250);
            MyInt c = new MyInt(-3535);
            MyInt d = new MyInt(-4000);

            Assert.AreEqual(a.substract(b).longValue(), -572);
            Assert.AreEqual(b.substract(a).longValue(), 572);
            Assert.AreEqual(c.substract(b).longValue(), -4785);
            Assert.AreEqual(b.substract(c).longValue(), 4785);
            Assert.AreEqual(d.substract(c).longValue(), -465);
            Assert.AreEqual(c.substract(d).longValue(), 465);
        }
Beispiel #6
0
        public void testDivide()
        {
            MyInt a = new MyInt(15760);
            MyInt b = new MyInt(30);
            MyInt c = new MyInt(-3056);
            MyInt d = new MyInt(0);
            MyInt e = new MyInt(-1);
            MyInt f = new MyInt(-50530);

            Assert.AreEqual(a.divide(b).longValue(), 525);
            Assert.AreEqual(b.divide(a).longValue(), 0);
            Assert.AreEqual(d.divide(e).longValue(), 0);
            Assert.AreEqual(c.divide(d).longValue(), 0);
            Assert.AreEqual(c.divide(c).longValue(), 1);
            Assert.AreEqual(f.divide(c).longValue(), 16);
            Assert.AreEqual(a.divide(c).longValue(), -5);
        }
Beispiel #7
0
        public void testMaxMin()
        {
            MyInt a = new MyInt(200);
            MyInt b = new MyInt(199);
            MyInt c = new MyInt("201");
            MyInt d = new MyInt(-200);

            Assert.AreEqual(b.max(a), a.max(b));
            Assert.AreEqual(a.max(b), a);
            Assert.AreEqual(c.max(a), c);
            Assert.AreEqual(c.max(b), c);
            Assert.AreEqual(b.max(d), b);
            Assert.AreEqual(c.max(c), c);

            Assert.AreEqual(b.min(d), d.min(b));
            Assert.AreEqual(a.min(b), b);
            Assert.AreEqual(a.min(c), a);
            Assert.AreEqual(d.min(a), d);
            Assert.AreEqual(c.min(d), d);
            Assert.AreEqual(c.min(c), c);
        }
Beispiel #8
0
        public void testGcd()
        {
            MyInt a = new MyInt(32);
            MyInt b = new MyInt(170);
            MyInt c = new MyInt(17);
            MyInt d = new MyInt(-50);
            MyInt f = new MyInt(-1);
            MyInt e = new MyInt(1);
            MyInt g = new MyInt(0);
            MyInt h = new MyInt(15);

            Assert.AreEqual(b.gcd(c).longValue(), 17);
            Assert.AreEqual(a.gcd(c).longValue(), 1);
            Assert.AreEqual(b.gcd(d).longValue(), 10);
            Assert.AreEqual(c.gcd(f).longValue(), 1);
            Assert.AreEqual(d.gcd(h).longValue(), 5);
            Assert.AreEqual(b.gcd(b).longValue(), 170);
            Assert.AreEqual(b.gcd(b.abs()).longValue(), 170);
            Assert.AreEqual(f.gcd(f).longValue(), 1);
            Assert.AreEqual(g.gcd(e).longValue(), 1);
        }
Beispiel #9
0
        public MyInt gcd(MyInt b)
        {
            MyInt a = abs();

            b = b.abs();
            if (a.compareTo(new MyInt(0)) || b.compareTo(new MyInt(0)))
            {
                return(new MyInt(1));
            }

            while (!a.compareTo(b))
            {
                if (a.max(b) == a)
                {
                    a = a.substract(b);
                }
                else
                {
                    b = b.substract(a);
                }
            }

            return(a);
        }
Beispiel #10
0
        public MyInt add(MyInt b)
        {
            byte[] valueA = getValue();
            byte[] valueB = b.getValue();

            byte[] result;

            bool isPositiveA = value[0] == 0;
            bool isPositiveB = valueB[0] == 0;

            if (isPositiveA == isPositiveB)
            {
                result = new byte[Math.Max(value.Length, valueB.Length) + 1];
                valueA = value.Skip(1).Reverse().ToArray();
                valueB = valueB.Skip(1).Reverse().ToArray();

                int temp = 0;
                for (int i = 0; i < result.Length; i++)
                {
                    int firstNum  = valueA.Length - 1 < i ? 0 : valueA[i];
                    int secondNum = valueB.Length - 1 < i ? 0 : valueB[i];
                    int sum       = firstNum + secondNum + temp;
                    result[i] = (byte)Math.Abs(sum % 10);
                    temp      = sum / 10;
                    if (i == result.Length - 1)
                    {
                        result[result.Length - 1] = (byte)(isPositiveA ? 0 : 1);
                    }
                }
            }
            else
            {
                result = new byte[Math.Max(value.Length, valueB.Length)];

                byte[] maxNum = abs().max(b.abs()).getValue();
                byte[] minNum = abs().min(b.abs()).getValue();

                bool resultIsPositive = abs().compareTo(new MyInt(maxNum)) ? valueA[0] == 0 : valueB[0] == 0;

                maxNum = maxNum.Skip(1).ToArray().Reverse().ToArray();
                minNum = minNum.Skip(1).ToArray().Reverse().ToArray();

                int temp = 0;
                for (int i = 0; i < result.Length; i++)
                {
                    int firstNum  = maxNum.Length - 1 < i ? 0 : maxNum[i];
                    int secondNum = minNum.Length - 1 < i ? 0 : minNum[i];
                    int diff      = firstNum - secondNum - temp;
                    result[i] = (byte)Math.Abs((diff + 10) % 10);
                    temp      = diff < 0 ? 1 : 0;
                    if (i == result.Length - 1)
                    {
                        result[result.Length - 1] = (byte)(resultIsPositive ? 0 : 1);
                    }
                }
            }


            result = result.Reverse().ToArray();

            return(new MyInt(result));
        }
Beispiel #11
0
 public bool compareTo(MyInt b)
 {
     return(value.SequenceEqual(b.getValue()));
 }
Beispiel #12
0
 public MyInt min(MyInt b)
 {
     return(max(b) == this ? b : this);
 }
Beispiel #13
0
        public MyInt divide(MyInt b)
        {
            if (b.compareTo(new MyInt(0)))
            {
                return(new MyInt(0));
            }

            byte[] valueA = getValue();
            byte[] valueB = b.getValue();


            List <byte> result = new List <byte>();

            bool isPositiveA = valueA[0] == 0;
            bool isPositiveB = valueB[0] == 0;

            valueA = valueA.Skip(1).ToArray();
            valueB = valueB.Skip(1).ToArray();

            int start = 0;
            int end   = 1;

            while (end <= valueA.Length)
            {
                List <byte> tempAValue = valueA.Skip(start).Take(end - start).ToList();;
                tempAValue.Insert(0, 0);

                List <byte> tempBValue = valueB.ToList();
                tempBValue.Insert(0, 0);

                MyInt tempA = new MyInt(tempAValue.ToArray());
                MyInt tempB = new MyInt(tempBValue.ToArray());

                if (tempA.max(tempB).compareTo(tempA))
                {
                    MyInt resultDivide = new MyInt(1);
                    while (!tempB.multiply(resultDivide).min(tempA).compareTo(tempA))
                    {
                        resultDivide = resultDivide.add(new MyInt(1));
                    }
                    if (!resultDivide.compareTo(new MyInt(1)))
                    {
                        resultDivide = resultDivide.substract(new MyInt(1));
                    }
                    result.AddRange(resultDivide.getValue().Skip(1));
                    MyInt resultMultiply   = resultDivide.multiply(tempB);
                    MyInt resultDivideRest = tempA.substract(resultMultiply);
                    start = end - resultDivideRest.getValue().Length + 1;
                    for (int i = 0; i + start < end; i++)
                    {
                        valueA[i + start] = resultDivideRest.getValue()[i + 1];
                    }
                }
                end++;
            }

            if (result.Count == 0)
            {
                result.Insert(0, 0);
            }

            result.Insert(0, (byte)(isPositiveA == isPositiveB ? 0 : 1));

            return(new MyInt(result.ToArray()));
        }
Beispiel #14
0
 public MyInt substract(MyInt b)
 {
     byte[] valueB = b.getValue();
     valueB[0] = (byte)(valueB[0] == 0 ? 1 : 0);
     return(add(new MyInt(valueB)));
 }