Example #1
0
        private static LongInteger CalculationSumAndSubstruct(LongInteger v1, LongInteger v2, int sign)
        {
            var result = v1.Clone();
            var i      = v1.Values.Count - 1;
            var j      = v2.Values.Count - 1;
            var hold   = 0;

            while (i >= 0 && (j >= 0 || hold != 0))
            {
                var res = result.Values[i] + (((j >= 0) ? v2.Values[j] : 0)) * sign + hold;
                hold             = (res + Constants.Radix) / Constants.Radix - 1;
                result.Values[i] = (res + Constants.Radix) % Constants.Radix;
                i--;
                j--;
            }

            if (sign > 0 && hold != 0)
            {
                result.Values.Insert(0, hold);
            }
            else
            {
                result.Normalize();
            }

            return(result);
        }
        public void TestDivZero()
        {
            var a = new LongInteger("12");
            var b = new LongInteger("-0");

            a.Div(b);
        }
Example #3
0
        /// <summary> Return "0" if (v1 = v2);  Return positive result if (v1 > v2);  Return negative result if (v2 > v1)</summary>>
        /// <param name="v1">compared value 1</param>
        /// <param name="v2"> compared value 2</param>
        public static int Compare(LongInteger v1, LongInteger v2)
        {
            if (Equals(v1, v2))
            {
                return(0);
            }

            var sign = ((v1.Negative) ? -1 : 1);

            if (v1.Negative != v2.Negative)
            {
                return(sign);
            }

            if (v1.Values.Count != v2.Values.Count)
            {
                return(sign * (v1.Values.Count - v2.Values.Count));
            }

            var i = 0;

            while (i < v1.Values.Count && v1.Values[i] == v2.Values[i])
            {
                i++;
            }
            return(sign * v1.Values[i].CompareTo(v2.Values[i]));
        }
        public void TestAryphOperations()
        {
            var a = new LongInteger("25");
            var b = new LongInteger("5");

            var c = a * b;

            Assert.AreEqual("25", a.ToString());
            Assert.AreEqual("5", b.ToString());
            Assert.AreEqual("125", c.ToString());

            c = a + b;

            Assert.AreEqual("25", a.ToString());
            Assert.AreEqual("5", b.ToString());
            Assert.AreEqual("30", c.ToString());

            c = a - b;

            Assert.AreEqual("25", a.ToString());
            Assert.AreEqual("5", b.ToString());
            Assert.AreEqual("20", c.ToString());

            c = a / b;

            Assert.AreEqual("25", a.ToString());
            Assert.AreEqual("5", b.ToString());
            Assert.AreEqual("5", c.ToString());

            c = a ^ 2;

            Assert.AreEqual("25", a.ToString());
            Assert.AreEqual("625", c.ToString());

            var d = a > b;

            Assert.AreEqual("25", a.ToString());
            Assert.AreEqual("5", b.ToString());
            Assert.AreEqual(true, d);

            d = a < b;

            Assert.AreEqual("25", a.ToString());
            Assert.AreEqual("5", b.ToString());
            Assert.AreEqual(false, d);

            d = a == b;

            Assert.AreEqual("25", a.ToString());
            Assert.AreEqual("5", b.ToString());
            Assert.AreEqual(false, d);

            b = new LongInteger("25");

            d = a == b;

            Assert.AreEqual("25", a.ToString());
            Assert.AreEqual("25", b.ToString());
            Assert.AreEqual(true, d);
        }
        public void Compare_NegativeDifferentLength_FirstLarger()
        {
            LongIntegerMath.Compare(LongInteger.Parse("225"), LongInteger.Parse("856"));
            var result = LongIntegerMath.Compare(LongInteger.Parse("-98895"), LongInteger.Parse("-85595455488432574564857"));

            Assert.IsTrue(result > 0);
        }
Example #6
0
        public void SumLongInteger_IncreasesSignificantBit()
        {
            var result = LongIntegerMath.SumLongInteger(LongInteger.Parse("9"), LongInteger.Parse("1"));

            Assert.AreEqual(LongInteger.Parse("10"), result);

            result = LongIntegerMath.SumLongInteger(LongInteger.Parse("99"), LongInteger.Parse("1"));
            Assert.AreEqual(LongInteger.Parse("100"), result);

            result = LongIntegerMath.SumLongInteger(LongInteger.Parse("999"), LongInteger.Parse("1"));
            Assert.AreEqual(LongInteger.Parse("1000"), result);

            result = LongIntegerMath.SumLongInteger(LongInteger.Parse("9999"), LongInteger.Parse("1"));
            Assert.AreEqual(LongInteger.Parse("10000"), result);

            result = LongIntegerMath.SumLongInteger(LongInteger.Parse("99999"), LongInteger.Parse("1"));
            Assert.AreEqual(LongInteger.Parse("100000"), result);

            result = LongIntegerMath.SumLongInteger(LongInteger.Parse("999999"), LongInteger.Parse("1"));
            Assert.AreEqual(LongInteger.Parse("1000000"), result);

            result = LongIntegerMath.SumLongInteger(LongInteger.Parse("9999999"), LongInteger.Parse("1"));
            Assert.AreEqual(LongInteger.Parse("10000000"), result);

            result = LongIntegerMath.SumLongInteger(LongInteger.Parse("99999999"), LongInteger.Parse("1"));
            Assert.AreEqual(LongInteger.Parse("100000000"), result);

            result = LongIntegerMath.SumLongInteger(LongInteger.Parse("999999999"), LongInteger.Parse("1"));
            Assert.AreEqual(LongInteger.Parse("1000000000"), result);

            result = LongIntegerMath.SumLongInteger(LongInteger.Parse("9999999999999999999999"), LongInteger.Parse("1"));
            Assert.AreEqual(LongInteger.Parse("10000000000000000000000"), result);
        }
Example #7
0
        public static LongInteger Module(LongInteger value)
        {
            var result = value.Clone();

            result.Negative = false;
            return(result);
        }
Example #8
0
        public static LongInteger OppositeValue(LongInteger value)
        {
            var result = value.Clone();

            result.InverSign();
            return(result);
        }
        public void TestFactorialNegative()
        {
            var a = new LongInteger("-453453456366656456");

            a.Factorial();

            Assert.AreEqual("-453453456366656456", a.ToString());
        }
Example #10
0
        public void SumLongInteger_ShouldNotChangeArguments()
        {
            var v1 = LongInteger.Parse("1");
            var v2 = LongInteger.Parse("200000000000000");
            var v3 = LongIntegerMath.SumLongInteger(v1, v2);

            Assert.AreEqual(v1.ToString(), "1");
            Assert.AreEqual(v2.ToString(), "200000000000000");
            Assert.AreEqual(v3.ToString(), "200000000000001");
        }
        public void MultiplicationLongInteger_WhithManyZeros()
        {
            var result = LongIntegerMath.MultiplicationLongInteger(LongInteger.Parse("786543767867865354"), LongInteger.Parse("100000000000000000"));

            Assert.AreEqual(LongInteger.Parse("78654376786786535400000000000000000"), result);

            result = LongIntegerMath.MultiplicationLongInteger(LongInteger.Parse("786543767867865354"), LongInteger.Parse("100000000000000086"));
            Assert.AreEqual(LongInteger.Parse("78654376786786603042764036636420444"), result);

            result = LongIntegerMath.MultiplicationLongInteger(LongInteger.Parse("78654376700000000007865354"), LongInteger.Parse("1000000000000080006"));
            Assert.AreEqual(LongInteger.Parse("78654376700006292829927614200000629275512124"), result);
        }
        public void TestTypeCasting()
        {
            var  a = new LongInteger("123");
            long b = a;

            Assert.AreEqual(b, 123);

            a = new LongInteger("-123");
            b = a;

            Assert.AreEqual(b, -123);
        }
        public void TestTypeCastingImplicit()
        {
            var a = new LongInteger("123");
            var b = (long)a;

            Assert.AreEqual(b, 123);

            a = new LongInteger("-123");
            b = a;

            Assert.AreEqual(b, -123);
        }
Example #14
0
        public static LongInteger Factorial(LongInteger value)
        {
            if (value.Negative)
            {
                throw new ArgumentException("Factorial x! is not defined for negative x");
            }
            var result = LongInteger.Parse("1");

            for (var i = LongInteger.Parse("1"); Compare(i, value) <= 0; i = SumLongInteger(i, LongInteger.Parse("1")))
            {
                result = MultiplicationLongInteger(result, i);
            }
            return(result);
        }
Example #15
0
        public static LongInteger MultiplicationLongInteger(LongInteger v1, LongInteger v2)
        {
            if (v1.Values.Count < v2.Values.Count)
            {
                MultiplicationLongInteger(v2, v1);
            }

            if (v1.Equals("0") || v2.Equals("0"))
            {
                return(LongInteger.Parse("0"));
            }

            var multResult = LongInteger.Parse("0");

            var j = v2.Values.Count - 1;
            var n = 0;

            while (j >= 0)
            {
                var result = Module(v1.Clone());
                var i      = v1.Values.Count - 1;
                var hold   = 0;
                while (i >= 0)
                {
                    var res = v1.Values[i] * v2.Values[j] + hold;
                    hold             = res / Constants.Radix;
                    result.Values[i] = res % Constants.Radix;
                    i--;
                }
                if (hold != 0)
                {
                    result.Values.Insert(0, hold);
                }

                for (var k = 0; k < n; k++)
                {
                    result.Values.Add(0);
                }

                multResult = SumLongInteger(multResult, result);
                j--;
                n++;
            }

            multResult.Negative = v1.Negative != v2.Negative;
            multResult.Normalize();
            return(multResult);
        }
Example #16
0
        public static ILongNumber Power(LongInteger value, LongInteger exponent)
        {
            var result = LongInteger.Parse("1");

            if (exponent.Negative)
            {
                return(DivisionLongInteger(result, Power(value, Module(exponent)) as LongInteger));
            }

            for (var i = LongInteger.Parse("1");
                 Compare(i, exponent) <= 0;
                 i = SumLongInteger(i, LongInteger.Parse("1")))
            {
                result = MultiplicationLongInteger(result, value);
            }
            return(result);
        }
        public void TestPow()
        {
            var a = new LongInteger("10");

            var c = a.Pow(2);

            Assert.AreEqual("10", a.ToString());
            Assert.AreEqual("100", c.ToString());

            a = new LongInteger("11");

            c = a.Pow(8);

            Assert.AreEqual("11", a.ToString());
            Assert.AreEqual("214358881", c.ToString());

            a = new LongInteger("-11");

            c = a.Pow(2);

            Assert.AreEqual("-11", a.ToString());
            Assert.AreEqual("121", c.ToString());

            a = new LongInteger("-11");

            c = a.Pow(3);

            Assert.AreEqual("-11", a.ToString());
            Assert.AreEqual("-1331", c.ToString());

            a = new LongInteger("23");

            c = a.Pow(0);

            Assert.AreEqual("23", a.ToString());
            Assert.AreEqual("1", c.ToString());


            a = new LongInteger("23");

            c = a.Pow(1);

            Assert.AreEqual("23", a.ToString());
            Assert.AreEqual("23", c.ToString());
        }
        public void TestCompare()
        {
            var a = new LongInteger("120");
            var b = new LongInteger("90");

            var r = LongInteger.Compare(a, b);

            Assert.AreEqual(1, r);

            a = new LongInteger("1110");
            b = new LongInteger("1120");

            r = LongInteger.Compare(a, b);

            Assert.AreEqual(-1, r);

            a = new LongInteger("1110");
            b = new LongInteger("1110");

            r = LongInteger.Compare(a, b);

            Assert.AreEqual(0, r);

            a = new LongInteger("-120");
            b = new LongInteger("90");

            r = LongInteger.Compare(a, b);

            Assert.AreEqual(-1, r);

            a = new LongInteger("120");
            b = new LongInteger("-290");

            r = LongInteger.Compare(a, b);

            Assert.AreEqual(1, r);

            a = new LongInteger("-320");
            b = new LongInteger("-290");

            r = LongInteger.Compare(a, b);

            Assert.AreEqual(-1, r);
        }
Example #19
0
        public static LongInteger SumLongInteger(LongInteger v1, LongInteger v2)
        {
            if (v1.Values.Count < v2.Values.Count)
            {
                return(SumLongInteger(v2, v1));
            }

            if (v1.Negative == v2.Negative)
            {
                return(CalculationSumAndSubstruct(v1, v2, 1));
            }

            if (v1.Negative)
            {
                return(OppositeValue(SubstructLongInteger(OppositeValue(v1), v2)));
            }

            return(SubstructLongInteger(v1, OppositeValue(v2)));
        }
        public void TestFactorial()
        {
            var a = new LongInteger("9");

            var c = a.Factorial();

            Assert.AreEqual("9", a.ToString());
            Assert.AreEqual("362880", c.ToString());

            a = new LongInteger("100");

            c = a.Factorial();

            Assert.AreEqual("100", a.ToString());
            Assert.AreEqual("93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000", c.ToString());

            a = new LongInteger("0");

            c = a.Factorial();

            Assert.AreEqual("0", a.ToString());
            Assert.AreEqual("1", c.ToString());
        }
Example #21
0
        public static ILongNumber DivisionIntoSmall(LongInteger v1, int v2)
        {
            if (v2 >= Constants.Radix)
            {
                var divider = LongInteger.Parse(Convert.ToString(v2));
                return(DivisionLongInteger(v1, divider));
            }

            if (v2 == 0)
            {
                return(v1.IsNegative() ? new InfinityLongNumber(true) : new InfinityLongNumber());
            }

            var result = v1.Clone();
            var modulo = 0;
            var i      = 0;

            while (i < v1.Values.Count)
            {
                var res = modulo * Constants.Radix + v1.Values[i];
                result.Values[i] = (res / Math.Abs(v2));
                modulo           = (res % Math.Abs(v2));
                i++;
            }

            if (v1.Negative || v2 < 0)
            {
                result.Negative = true;
            }
            if (v1.Negative && v2 < 0)
            {
                result.Negative = false;
            }

            result.Normalize();
            return(result);
        }
Example #22
0
        public static ILongNumber DivisionLongInteger(LongInteger v1, LongInteger v2)
        {
            ILongNumber result           = Module(v1);
            var         modulo           = Module(v2);
            var         resultIsNegative = v1.IsNegative() != v2.IsNegative();

            if (v2.Values.Count == 1)
            {
                result = DivisionIntoSmall(result as LongInteger, v2.Values[0]);
            }
            else
            {
                var low = 0;
                var up  = Constants.Radix;
                while ((up - low) > 1)
                {
                    var midRange = (low + up) / 2;
                    var res      = MultiplicationLongInteger(modulo, LongInteger.Parse(Convert.ToString(midRange)));
                    if (Compare(res, modulo) > 0)
                    {
                        //                        up = up - up/2;
                        up = midRange;
                    }
                    else
                    {
                        //                        low = low + up/2;
                        low = midRange;
                    }
                }
                throw new NotImplementedException();
            }
            if (resultIsNegative)
            {
                result.InverSign();
            }
            return(result);
        }
Example #23
0
        public static LongInteger SubstructLongInteger(LongInteger v1, LongInteger v2)
        {
            if (Compare(v1, v2) == 0)
            {
                return(LongInteger.Parse("0"));
            }

            if (v1.Negative)
            {
                return(OppositeValue(SumLongInteger(v2, OppositeValue(v1))));
            }

            if (v2.Negative)
            {
                return(SumLongInteger(v1, OppositeValue(v2)));
            }

            if (Compare(v1, v2) < 0)
            {
                return(OppositeValue(SubstructLongInteger(v2, v1)));
            }

            return(CalculationSumAndSubstruct(v1, v2, -1));
        }
 public void Create_Liter()
 {
     LongInteger.Parse("15fd");
 }
        public void Create_NegativeIntegerManyZeroes()
        {
            var number = LongInteger.Parse("-450000000000000000000000000025");

            Assert.AreEqual("-450000000000000000000000000025", number.ToString());
        }
        public void Create_Integer()
        {
            var number = LongInteger.Parse("450025");

            Assert.AreEqual("450025", number.ToString());
        }
        public void Create_Positive()
        {
            var number = LongInteger.Parse("1");

            Assert.AreEqual("1", number.ToString());
        }
        public void Create_NegativeStartsWhithManyZero()
        {
            var number = LongInteger.Parse("-0000000000000005894");

            Assert.AreEqual("-5894", number.ToString());
        }
        public void Create_StartsWhithZero()
        {
            var number = LongInteger.Parse("005894");

            Assert.AreEqual("5894", number.ToString());
        }
        public void Create_NegativeZero()
        {
            var number = LongInteger.Parse("-0");

            Assert.AreEqual("0", number.ToString());
        }