Exemplo n.º 1
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);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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));
        }