/// <summary>
        /// Composite N-point approximation of the definite integral in the provided interval by Simpson's rule.
        /// </summary>
        public double IntegrateComposite(
            CustomFunction f,
            double intervalBegin,
            double intervalEnd,
            int numberOfPartitions)
        {
            if (numberOfPartitions <= 0)
            {
                throw new ArgumentOutOfRangeException("numberOfPartitions", Properties.LocalStrings.ArgumentPositive);
            }

            if (IntegerTheory.IsOdd(numberOfPartitions))
            {
                throw new ArgumentException(Properties.LocalStrings.ArgumentEven, "numberOfPartitions");
            }

            double step   = (intervalEnd - intervalBegin) / numberOfPartitions;
            double factor = step / 3;

            double offset = step;
            int    m      = 4;
            double sum    = f(intervalBegin) + f(intervalEnd);

            for (int i = 0; i < numberOfPartitions - 1; i++)
            {
                // NOTE (ruegg, 2009-01-07): Do not combine intervalBegin and offset (numerical stability!)
                sum    += m * f(intervalBegin + offset);
                m       = 6 - m;
                offset += step;
            }

            return(factor * sum);
        }
        public void TestIsPowerOfTwo64()
        {
            for (int i = 2; i < 63; i++)
            {
                long x = ((long)1) << i;
                Assert.IsTrue(IntegerTheory.IsPowerOfTwo(x), x + " (+)");
                Assert.IsFalse(IntegerTheory.IsPowerOfTwo(x - 1), x + "-1 (-)");
                Assert.IsFalse(IntegerTheory.IsPowerOfTwo(x + 1), x + "+1 (-)");
                Assert.IsFalse(IntegerTheory.IsPowerOfTwo(-x), "-" + x + " (-)");
                Assert.IsFalse(IntegerTheory.IsPowerOfTwo(-x + 1), "-" + x + "+1 (-)");
                Assert.IsFalse(IntegerTheory.IsPowerOfTwo(-x - 1), "-" + x + "-1 (-)");
            }

            Assert.IsTrue(IntegerTheory.IsPowerOfTwo((long)4), "4 (+)");
            Assert.IsFalse(IntegerTheory.IsPowerOfTwo((long)3), "3 (-)");
            Assert.IsTrue(IntegerTheory.IsPowerOfTwo((long)2), "2 (+)");
            Assert.IsTrue(IntegerTheory.IsPowerOfTwo((long)1), "1 (+)");
            Assert.IsFalse(IntegerTheory.IsPowerOfTwo((long)0), "0 (-)");
            Assert.IsFalse(IntegerTheory.IsPowerOfTwo((long)-1), "-1 (-)");
            Assert.IsFalse(IntegerTheory.IsPowerOfTwo((long)-2), "-2 (-)");
            Assert.IsFalse(IntegerTheory.IsPowerOfTwo((long)-3), "-3 (-)");
            Assert.IsFalse(IntegerTheory.IsPowerOfTwo((long)-4), "-4 (-)");
            Assert.IsFalse(IntegerTheory.IsPowerOfTwo(Int64.MinValue), "Int32.MinValue (-)");
            Assert.IsFalse(IntegerTheory.IsPowerOfTwo(Int64.MinValue + 1), "Int32.MinValue+1 (-)");
            Assert.IsFalse(IntegerTheory.IsPowerOfTwo(Int64.MaxValue), "Int32.MaxValue (-)");
            Assert.IsFalse(IntegerTheory.IsPowerOfTwo(Int64.MaxValue - 1), "Int32.MaxValue-1 (-)");
        }
Ejemplo n.º 3
0
 public void LcmSupportsLargeInput()
 {
     Assert.AreEqual(Int32.MaxValue, IntegerTheory.LeastCommonMultiple(Int32.MaxValue, Int32.MaxValue), "Lcm(Int32Max,Int32Max)");
     Assert.AreEqual(Int64.MaxValue, IntegerTheory.LeastCommonMultiple(Int64.MaxValue, Int64.MaxValue), "Lcm(Int64Max,Int64Max)");
     Assert.AreEqual(Int64.MaxValue, IntegerTheory.LeastCommonMultiple(-Int64.MaxValue, -Int64.MaxValue), "Lcm(-Int64Max,-Int64Max)");
     Assert.AreEqual(Int64.MaxValue, IntegerTheory.LeastCommonMultiple(-Int64.MaxValue, Int64.MaxValue), "Lcm(-Int64Max,Int64Max)");
 }
        public void TestIsPerfectSquare64()
        {
            // Test all known suares
            for (int i = 0; i < 32; i++)
            {
                long t = ((long)1) << i;
                Assert.IsTrue(IntegerTheory.IsPerfectSquare(t * t), t + "^2 (+)");
            }

            // Test 1-offset from all known squares
            for (int i = 1; i < 32; i++)
            {
                long t = ((long)1) << i;
                Assert.IsFalse(IntegerTheory.IsPerfectSquare((t * t) - 1), t + "^2-1 (-)");
                Assert.IsFalse(IntegerTheory.IsPerfectSquare((t * t) + 1), t + "^2+1 (-)");
            }

            // Selected Cases
            Assert.IsTrue(IntegerTheory.IsPerfectSquare((long)1000000000000000000), "1000000000000000000 (+)");
            Assert.IsFalse(IntegerTheory.IsPerfectSquare((long)1000000000000000001), "1000000000000000001 (-)");
            Assert.IsFalse(IntegerTheory.IsPerfectSquare((long)999999999999999999), "999999999999999999 (-)");
            Assert.IsFalse(IntegerTheory.IsPerfectSquare((long)999999999999999993), "999999999999999993 (-)");
            Assert.IsFalse(IntegerTheory.IsPerfectSquare((long)-4), "-4 (-)");
            Assert.IsFalse(IntegerTheory.IsPerfectSquare(Int64.MinValue), "Int32.MinValue (-)");
            Assert.IsFalse(IntegerTheory.IsPerfectSquare(Int64.MaxValue), "Int32.MaxValue (-)");
            Assert.IsTrue(IntegerTheory.IsPerfectSquare((long)1), "1 (+)");
            Assert.IsTrue(IntegerTheory.IsPerfectSquare((long)0), "0 (+)");
            Assert.IsFalse(IntegerTheory.IsPerfectSquare((long)-1), "-1 (-)");
        }
Ejemplo n.º 5
0
 public void GcdHandlesNegativeInputCorrectly()
 {
     Assert.AreEqual((BigInteger)5, IntegerTheory.GreatestCommonDivisor((BigInteger)(-5), 0), "Gcd(-5,0)");
     Assert.AreEqual((BigInteger)5, IntegerTheory.GreatestCommonDivisor(BigInteger.Zero, -5), "Gcd(0, -5)");
     Assert.AreEqual((BigInteger)1, IntegerTheory.GreatestCommonDivisor((BigInteger)(-7), 15), "Gcd(-7,15)");
     Assert.AreEqual((BigInteger)1, IntegerTheory.GreatestCommonDivisor((BigInteger)(-7), -15), "Gcd(-7,-15)");
 }
Ejemplo n.º 6
0
        UpperMedian(IEnumerable <double> samples)
        {
            List <double> list          = new List <double>(samples);
            int           lowerMidpoint = IntegerTheory.IsEven(list.Count) ? ((list.Count / 2) + 1) : (list.Count / 2);

            return(OrderSelect(list, 0, list.Count - 1, lowerMidpoint + 1));
        }
        public void TestIsPerfectSquare32()
        {
            // Test all known suares
            int lastRadix = (int)Math.Floor(Math.Sqrt(Int32.MaxValue));

            for (int i = 0; i <= lastRadix; i++)
            {
                Assert.IsTrue(IntegerTheory.IsPerfectSquare(i * i), i + "^2 (+)");
            }

            // Test 1-offset from all known squares
            for (int i = 2; i <= lastRadix; i++)
            {
                Assert.IsFalse(IntegerTheory.IsPerfectSquare((i * i) - 1), i + "^2-1 (-)");
                Assert.IsFalse(IntegerTheory.IsPerfectSquare((i * i) + 1), i + "^2+1 (-)");
            }

            // Selected Cases
            Assert.IsTrue(IntegerTheory.IsPerfectSquare(100000000), "100000000 (+)");
            Assert.IsFalse(IntegerTheory.IsPerfectSquare(100000001), "100000001 (-)");
            Assert.IsFalse(IntegerTheory.IsPerfectSquare(99999999), "99999999 (-)");
            Assert.IsFalse(IntegerTheory.IsPerfectSquare(-4), "-4 (-)");
            Assert.IsFalse(IntegerTheory.IsPerfectSquare(Int32.MinValue), "Int32.MinValue (-)");
            Assert.IsFalse(IntegerTheory.IsPerfectSquare(Int32.MaxValue), "Int32.MaxValue (-)");
            Assert.IsTrue(IntegerTheory.IsPerfectSquare(1), "1 (+)");
            Assert.IsTrue(IntegerTheory.IsPerfectSquare(0), "0 (+)");
            Assert.IsFalse(IntegerTheory.IsPerfectSquare(-1), "-1 (-)");
        }
Ejemplo n.º 8
0
 public void GcdHandlesNegativeInputCorrectly()
 {
     Assert.AreEqual(5, IntegerTheory.GreatestCommonDivisor(-5, 0), "Gcd(-5,0)");
     Assert.AreEqual(5, IntegerTheory.GreatestCommonDivisor(0, -5), "Gcd(0, -5)");
     Assert.AreEqual(1, IntegerTheory.GreatestCommonDivisor(-7, 15), "Gcd(-7,15)");
     Assert.AreEqual(1, IntegerTheory.GreatestCommonDivisor(-7, -15), "Gcd(-7,-15)");
 }
 public void PowerOfTwoMatchesFloatingPointPower64()
 {
     for (int i = 0; i < 63; i++)
     {
         Assert.AreEqual(Math.Round(Math.Pow(2, i)), IntegerTheory.PowerOfTwo((long)i));
     }
 }
Ejemplo n.º 10
0
 public void GcdSupportsLargeInput()
 {
     Assert.AreEqual(Int32.MaxValue, IntegerTheory.GreatestCommonDivisor(0, Int32.MaxValue), "Gcd(0,Int32Max)");
     Assert.AreEqual(Int64.MaxValue, IntegerTheory.GreatestCommonDivisor(0, Int64.MaxValue), "Gcd(0,Int64Max)");
     Assert.AreEqual(1, IntegerTheory.GreatestCommonDivisor(Int32.MaxValue, Int64.MaxValue), "Gcd(Int32Max,Int64Max)");
     Assert.AreEqual(1 << 18, IntegerTheory.GreatestCommonDivisor(1 << 18, 1 << 20), "Gcd(1>>18,1<<20)");
 }
        public void TestIsPowerOfTwo32()
        {
            for (int i = 2; i < 31; i++)
            {
                int x = 1 << i;
                Assert.IsTrue(IntegerTheory.IsPowerOfTwo(x), x + " (+)");
                Assert.IsFalse(IntegerTheory.IsPowerOfTwo(x - 1), x + "-1 (-)");
                Assert.IsFalse(IntegerTheory.IsPowerOfTwo(x + 1), x + "+1 (-)");
                Assert.IsFalse(IntegerTheory.IsPowerOfTwo(-x), "-" + x + " (-)");
                Assert.IsFalse(IntegerTheory.IsPowerOfTwo(-x + 1), "-" + x + "+1 (-)");
                Assert.IsFalse(IntegerTheory.IsPowerOfTwo(-x - 1), "-" + x + "-1 (-)");
            }

            Assert.IsTrue(IntegerTheory.IsPowerOfTwo(4), "4 (+)");
            Assert.IsFalse(IntegerTheory.IsPowerOfTwo(3), "3 (-)");
            Assert.IsTrue(IntegerTheory.IsPowerOfTwo(2), "2 (+)");
            Assert.IsTrue(IntegerTheory.IsPowerOfTwo(1), "1 (+)");
            Assert.IsFalse(IntegerTheory.IsPowerOfTwo(0), "0 (-)");
            Assert.IsFalse(IntegerTheory.IsPowerOfTwo(-1), "-1 (-)");
            Assert.IsFalse(IntegerTheory.IsPowerOfTwo(-2), "-2 (-)");
            Assert.IsFalse(IntegerTheory.IsPowerOfTwo(-3), "-3 (-)");
            Assert.IsFalse(IntegerTheory.IsPowerOfTwo(-4), "-4 (-)");
            Assert.IsFalse(IntegerTheory.IsPowerOfTwo(Int32.MinValue), "Int32.MinValue (-)");
            Assert.IsFalse(IntegerTheory.IsPowerOfTwo(Int32.MinValue + 1), "Int32.MinValue+1 (-)");
            Assert.IsFalse(IntegerTheory.IsPowerOfTwo(Int32.MaxValue), "Int32.MaxValue (-)");
            Assert.IsFalse(IntegerTheory.IsPowerOfTwo(Int32.MaxValue - 1), "Int32.MaxValue-1 (-)");
        }
Ejemplo n.º 12
0
 public void ListLcmHandlesNormalInputCorrectly()
 {
     Assert.AreEqual(120, IntegerTheory.LeastCommonMultiple(-10, 6, -8), "Lcm(-10,6,-8)");
     Assert.AreEqual(4680, IntegerTheory.LeastCommonMultiple(-10, 6, -8, 5, 9, 13), "Lcm(-10,6,-8,5,9,13)");
     Assert.AreEqual(3000, IntegerTheory.LeastCommonMultiple(-10, 20, 120, 60, -15, 1000), "Lcm(-10,20,120,60,-15,1000)");
     Assert.AreEqual(984, IntegerTheory.LeastCommonMultiple(492, -2 * 492, 492 / 4), "Lcm(492, -984, 123)");
     Assert.AreEqual(2016, IntegerTheory.LeastCommonMultiple(32, 42, 36, 18), "Lcm(32,42,36,18)");
 }
Ejemplo n.º 13
0
 public void ListGcdHandlesNormalInputCorrectly()
 {
     Assert.AreEqual(2, IntegerTheory.GreatestCommonDivisor(-10, 6, -8), "Gcd(-10,6,-8)");
     Assert.AreEqual(1, IntegerTheory.GreatestCommonDivisor(-10, 6, -8, 5, 9, 13), "Gcd(-10,6,-8,5,9,13)");
     Assert.AreEqual(5, IntegerTheory.GreatestCommonDivisor(-10, 20, 120, 60, -15, 1000), "Gcd(-10,20,120,60,-15,1000)");
     Assert.AreEqual(3, IntegerTheory.GreatestCommonDivisor(Int64.MaxValue - 1, Int64.MaxValue - 4, Int64.MaxValue - 7), "Gcd(Int64Max-1,Int64Max-4,Int64Max-7)");
     Assert.AreEqual(123, IntegerTheory.GreatestCommonDivisor(492, -2 * 492, 492 / 4), "Gcd(492, -984, 123)");
 }
Ejemplo n.º 14
0
 public void LcmSupportsLargeInput()
 {
     Assert.AreEqual((BigInteger)Int32.MaxValue, IntegerTheory.LeastCommonMultiple((BigInteger)Int32.MaxValue, Int32.MaxValue), "Lcm(Int32Max,Int32Max)");
     Assert.AreEqual((BigInteger)Int64.MaxValue, IntegerTheory.LeastCommonMultiple((BigInteger)Int64.MaxValue, Int64.MaxValue), "Lcm(Int64Max,Int64Max)");
     Assert.AreEqual((BigInteger)Int64.MaxValue, IntegerTheory.LeastCommonMultiple((BigInteger)(-Int64.MaxValue), -Int64.MaxValue), "Lcm(-Int64Max,-Int64Max)");
     Assert.AreEqual((BigInteger)Int64.MaxValue, IntegerTheory.LeastCommonMultiple((BigInteger)(-Int64.MaxValue), Int64.MaxValue), "Lcm(-Int64Max,Int64Max)");
     Assert.AreEqual(BigInteger.Parse("91739176367857263082719902034485224119528064014300888465614024"), IntegerTheory.LeastCommonMultiple(BigInteger.Parse("7305316061155559483748611586449542122662"), BigInteger.Parse("57377277362010117405715236427413896")), "Lcm(large)");
 }
Ejemplo n.º 15
0
 public void GcdHandlesNormalInputCorrectly()
 {
     Assert.AreEqual((BigInteger)0, IntegerTheory.GreatestCommonDivisor(BigInteger.Zero, BigInteger.Zero), "Gcd(0,0)");
     Assert.AreEqual((BigInteger)6, IntegerTheory.GreatestCommonDivisor(BigInteger.Zero, 6), "Gcd(0,6)");
     Assert.AreEqual((BigInteger)1, IntegerTheory.GreatestCommonDivisor((BigInteger)7, 13), "Gcd(7,13)");
     Assert.AreEqual((BigInteger)7, IntegerTheory.GreatestCommonDivisor((BigInteger)7, 14), "Gcd(7,14)");
     Assert.AreEqual((BigInteger)1, IntegerTheory.GreatestCommonDivisor((BigInteger)7, 15), "Gcd(7,15)");
     Assert.AreEqual((BigInteger)3, IntegerTheory.GreatestCommonDivisor((BigInteger)6, 15), "Gcd(6,15)");
 }
 public void CeilingToPowerOfTwoReturnsZeroForNegativeNumbers64()
 {
     Assert.AreEqual(0, IntegerTheory.CeilingToPowerOfTwo((long)-1), "-1");
     Assert.AreEqual(0, IntegerTheory.CeilingToPowerOfTwo((long)-2), "-2");
     Assert.AreEqual(0, IntegerTheory.CeilingToPowerOfTwo((long)-3), "-3");
     Assert.AreEqual(0, IntegerTheory.CeilingToPowerOfTwo((long)-4), "-4");
     Assert.AreEqual(0, IntegerTheory.CeilingToPowerOfTwo(Int64.MinValue), "Int64.MinValue");
     Assert.AreEqual(0, IntegerTheory.CeilingToPowerOfTwo(Int64.MinValue + 1), "Int64.MinValue+1");
 }
Ejemplo n.º 17
0
 public void GcdSupportsLargeInput()
 {
     Assert.AreEqual((BigInteger)Int32.MaxValue, IntegerTheory.GreatestCommonDivisor(BigInteger.Zero, Int32.MaxValue), "Gcd(0,Int32Max)");
     Assert.AreEqual((BigInteger)Int64.MaxValue, IntegerTheory.GreatestCommonDivisor(BigInteger.Zero, Int64.MaxValue), "Gcd(0,Int64Max)");
     Assert.AreEqual((BigInteger)1, IntegerTheory.GreatestCommonDivisor((BigInteger)Int32.MaxValue, Int64.MaxValue), "Gcd(Int32Max,Int64Max)");
     Assert.AreEqual((BigInteger)(1 << 18), IntegerTheory.GreatestCommonDivisor((BigInteger)(1 << 18), 1 << 20), "Gcd(1>>18,1<<20)");
     Assert.AreEqual((BigInteger)(1 << 18), IntegerTheory.GreatestCommonDivisor((BigInteger)(1 << 18), 1 << 20), "Gcd(1>>18,1<<20)");
     Assert.AreEqual((BigInteger)4569031055798, IntegerTheory.GreatestCommonDivisor(BigInteger.Parse("7305316061155559483748611586449542122662"), BigInteger.Parse("57377277362010117405715236427413896")), "Gcd(large)");
 }
Ejemplo n.º 18
0
 public void GcdHandlesNormalInputCorrectly()
 {
     Assert.AreEqual(0, IntegerTheory.GreatestCommonDivisor(0, 0), "Gcd(0,0)");
     Assert.AreEqual(6, IntegerTheory.GreatestCommonDivisor(0, 6), "Gcd(0,6)");
     Assert.AreEqual(1, IntegerTheory.GreatestCommonDivisor(7, 13), "Gcd(7,13)");
     Assert.AreEqual(7, IntegerTheory.GreatestCommonDivisor(7, 14), "Gcd(7,14)");
     Assert.AreEqual(1, IntegerTheory.GreatestCommonDivisor(7, 15), "Gcd(7,15)");
     Assert.AreEqual(3, IntegerTheory.GreatestCommonDivisor(6, 15), "Gcd(6,15)");
 }
Ejemplo n.º 19
0
        /// <summary>
        /// Run example
        /// </summary>
        public void Run()
        {
            // 1. Find out whether the provided number is an even number
            Console.WriteLine(@"1. Find out whether the provided number is an even number");
            Console.WriteLine(@"{0} is even = {1}. {2} is even = {3}", 1, IntegerTheory.IsEven(1), 2, 2.IsEven());
            Console.WriteLine();

            // 2. Find out whether the provided number is an odd number
            Console.WriteLine(@"2. Find out whether the provided number is an odd number");
            Console.WriteLine(@"{0} is odd = {1}. {2} is odd = {3}", 1, 1.IsOdd(), 2, IntegerTheory.IsOdd(2));
            Console.WriteLine();

            // 3. Find out whether the provided number is a perfect power of two
            Console.WriteLine(@"2. Find out whether the provided number is a perfect power of two");
            Console.WriteLine(@"{0} is power of two = {1}. {2} is power of two = {3}", 5, 5.IsPowerOfTwo(), 16, IntegerTheory.IsPowerOfTwo(16));
            Console.WriteLine();

            // 4. Find the closest perfect power of two that is larger or equal to 97
            Console.WriteLine(@"4. Find the closest perfect power of two that is larger or equal to 97");
            Console.WriteLine(97.CeilingToPowerOfTwo());
            Console.WriteLine();

            // 5. Raise 2 to the 16
            Console.WriteLine(@"5. Raise 2 to the 16");
            Console.WriteLine(16.PowerOfTwo());
            Console.WriteLine();

            // 6. Find out whether the number is a perfect square
            Console.WriteLine(@"6. Find out whether the number is a perfect square");
            Console.WriteLine(@"{0} is perfect square = {1}. {2} is perfect square = {3}", 37, 37.IsPerfectSquare(), 81, IntegerTheory.IsPerfectSquare(81));
            Console.WriteLine();

            // 7. Compute the greatest common divisor of 32 and 36
            Console.WriteLine(@"7. Returns the greatest common divisor of 32 and 36");
            Console.WriteLine(IntegerTheory.GreatestCommonDivisor(32, 36));
            Console.WriteLine();

            // 8. Compute the greatest common divisor of 492, -984, 123, 246
            Console.WriteLine(@"8. Returns the greatest common divisor of 492, -984, 123, 246");
            Console.WriteLine(IntegerTheory.GreatestCommonDivisor(492, -984, 123, 246));
            Console.WriteLine();

            // 9. Compute the extended greatest common divisor "z", such that 45*x + 18*y = z
            Console.WriteLine(@"9. Compute the extended greatest common divisor Z, such that 45*x + 18*y = Z");
            long x, y;
            var  z = IntegerTheory.ExtendedGreatestCommonDivisor(45, 18, out x, out y);

            Console.WriteLine(@"z = {0}, x = {1}, y = {2}. 45*{1} + 18*{2} = {0}", z, x, y);
            Console.WriteLine();

            // 10. Compute the least common multiple of 16 and 12
            Console.WriteLine(@"10. Compute the least common multiple of 16 and 12");
            Console.WriteLine(IntegerTheory.LeastCommonMultiple(16, 12));
            Console.WriteLine();
        }
Ejemplo n.º 20
0
        public void LcmHandlesNormalInputCorrectly()
        {
            Assert.AreEqual((BigInteger)10, IntegerTheory.LeastCommonMultiple((BigInteger)10, 10), "Lcm(10,10)");

            Assert.AreEqual((BigInteger)0, IntegerTheory.LeastCommonMultiple(BigInteger.Zero, 10), "Lcm(0,10)");
            Assert.AreEqual((BigInteger)0, IntegerTheory.LeastCommonMultiple((BigInteger)10, 0), "Lcm(10,0)");

            Assert.AreEqual((BigInteger)77, IntegerTheory.LeastCommonMultiple((BigInteger)11, 7), "Lcm(11,7)");
            Assert.AreEqual((BigInteger)33, IntegerTheory.LeastCommonMultiple((BigInteger)11, 33), "Lcm(11,33)");
            Assert.AreEqual((BigInteger)374, IntegerTheory.LeastCommonMultiple((BigInteger)11, 34), "Lcm(11,34)");
        }
Ejemplo n.º 21
0
        public void LcmHandlesNormalInputCorrectly()
        {
            Assert.AreEqual(10, IntegerTheory.LeastCommonMultiple(10, 10), "Lcm(10,10)");

            Assert.AreEqual(0, IntegerTheory.LeastCommonMultiple(0, 10), "Lcm(0,10)");
            Assert.AreEqual(0, IntegerTheory.LeastCommonMultiple(10, 0), "Lcm(10,0)");

            Assert.AreEqual(77, IntegerTheory.LeastCommonMultiple(11, 7), "Lcm(11,7)");
            Assert.AreEqual(33, IntegerTheory.LeastCommonMultiple(11, 33), "Lcm(11,33)");
            Assert.AreEqual(374, IntegerTheory.LeastCommonMultiple(11, 34), "Lcm(11,34)");
        }
Ejemplo n.º 22
0
        public void ExtendedGcdHandlesNormalInputCorrectly()
        {
            long x, y;

            Assert.AreEqual(3, IntegerTheory.ExtendedGreatestCommonDivisor(6, 15, out x, out y), "Egcd(6,15)");
            Assert.AreEqual(3, (6 * x) + (15 * y), "Egcd(6,15) -> a*x+b*y");

            Assert.AreEqual(3, IntegerTheory.ExtendedGreatestCommonDivisor(-6, 15, out x, out y), "Egcd(-6,15)");
            Assert.AreEqual(3, (-6 * x) + (15 * y), "Egcd(-6,15) -> a*x+b*y");

            Assert.AreEqual(3, IntegerTheory.ExtendedGreatestCommonDivisor(-6, -15, out x, out y), "Egcd(-6,-15)");
            Assert.AreEqual(3, (-6 * x) + (-15 * y), "Egcd(-6,-15) -> a*x+b*y");
        }
        public void CeilingToPowerOfTwoThrowsWhenResultWouldOverflow64()
        {
            Assert.Throws(
                typeof(ArgumentOutOfRangeException),
                () => IntegerTheory.CeilingToPowerOfTwo(Int64.MaxValue));

            const long maxPowerOfTwo = 0x4000000000000000;

            Assert.Throws(
                typeof(ArgumentOutOfRangeException),
                () => IntegerTheory.CeilingToPowerOfTwo(maxPowerOfTwo + 1));

            Assert.DoesNotThrow(
                () => IntegerTheory.CeilingToPowerOfTwo(maxPowerOfTwo - 1));
        }
        public void TestEvenOdd32()
        {
            Assert.IsTrue(IntegerTheory.IsEven(0), "0 is even");
            Assert.IsFalse(IntegerTheory.IsOdd(0), "0 is not odd");

            Assert.IsFalse(IntegerTheory.IsEven(1), "1 is not even");
            Assert.IsTrue(IntegerTheory.IsOdd(1), "1 is odd");

            Assert.IsFalse(IntegerTheory.IsEven(-1), "-1 is not even");
            Assert.IsTrue(IntegerTheory.IsOdd(-1), "-1 is odd");

            Assert.IsFalse(IntegerTheory.IsEven(Int32.MaxValue), "Int32.Max is not even");
            Assert.IsTrue(IntegerTheory.IsOdd(Int32.MaxValue), "Int32.Max is odd");

            Assert.IsTrue(IntegerTheory.IsEven(Int32.MinValue), "Int32.Min is even");
            Assert.IsFalse(IntegerTheory.IsOdd(Int32.MinValue), "Int32.Min is not odd");
        }
        public void TestEvenOdd64()
        {
            Assert.IsTrue(IntegerTheory.IsEven((long)0), "0 is even");
            Assert.IsFalse(IntegerTheory.IsOdd((long)0), "0 is not odd");

            Assert.IsFalse(IntegerTheory.IsEven((long)1), "1 is not even");
            Assert.IsTrue(IntegerTheory.IsOdd((long)1), "1 is odd");

            Assert.IsFalse(IntegerTheory.IsEven((long)-1), "-1 is not even");
            Assert.IsTrue(IntegerTheory.IsOdd((long)-1), "-1 is odd");

            Assert.IsFalse(IntegerTheory.IsEven(Int64.MaxValue), "Int64.Max is not even");
            Assert.IsTrue(IntegerTheory.IsOdd(Int64.MaxValue), "Int64.Max is odd");

            Assert.IsTrue(IntegerTheory.IsEven(Int64.MinValue), "Int64.Min is even");
            Assert.IsFalse(IntegerTheory.IsOdd(Int64.MinValue), "Int64.Min is not odd");
        }
Ejemplo n.º 26
0
        public void ExtendedGcdHandlesNormalInputCorrectly()
        {
            BigInteger x, y;

            Assert.AreEqual((BigInteger)3, IntegerTheory.ExtendedGreatestCommonDivisor(6, 15, out x, out y), "Egcd(6,15)");
            Assert.AreEqual((BigInteger)3, (6 * x) + (15 * y), "Egcd(6,15) -> a*x+b*y");

            Assert.AreEqual((BigInteger)3, IntegerTheory.ExtendedGreatestCommonDivisor(-6, 15, out x, out y), "Egcd(-6,15)");
            Assert.AreEqual((BigInteger)3, (-6 * x) + (15 * y), "Egcd(-6,15) -> a*x+b*y");

            Assert.AreEqual((BigInteger)3, IntegerTheory.ExtendedGreatestCommonDivisor(-6, -15, out x, out y), "Egcd(-6,-15)");
            Assert.AreEqual((BigInteger)3, (-6 * x) + (-15 * y), "Egcd(-6,-15) -> a*x+b*y");

            var a = BigInteger.Parse("7305316061155559483748611586449542122662");
            var b = BigInteger.Parse("57377277362010117405715236427413896");

            Assert.AreEqual((BigInteger)4569031055798, IntegerTheory.ExtendedGreatestCommonDivisor(a, b, out x, out y), "Egcd(large)");
            Assert.AreEqual((BigInteger)4569031055798, (a * x) + (b * y), "Egcd(large) -> a*x+b*y");
            Assert.AreEqual((BigInteger)4569031055798, IntegerTheory.ExtendedGreatestCommonDivisor(-a, b, out x, out y), "Egcd(-large)");
            Assert.AreEqual((BigInteger)4569031055798, (-a * x) + (b * y), "Egcd(-large) -> a*x+b*y");
        }
        public void CeilingToPowerOfHandlesPositiveIntegersCorrectly64()
        {
            Assert.AreEqual(0, IntegerTheory.CeilingToPowerOfTwo((long)0), "0");
            Assert.AreEqual(1, IntegerTheory.CeilingToPowerOfTwo((long)1), "1");
            Assert.AreEqual(2, IntegerTheory.CeilingToPowerOfTwo((long)2), "2");
            Assert.AreEqual(4, IntegerTheory.CeilingToPowerOfTwo((long)3), "3");
            Assert.AreEqual(4, IntegerTheory.CeilingToPowerOfTwo((long)4), "4");

            for (int i = 2; i < 63; i++)
            {
                long x = ((long)1) << i;
                Assert.AreEqual(x, IntegerTheory.CeilingToPowerOfTwo(x), x.ToString());
                Assert.AreEqual(x, IntegerTheory.CeilingToPowerOfTwo(x - 1), x + "-1");
                Assert.AreEqual(x, IntegerTheory.CeilingToPowerOfTwo((x >> 1) + 1), x + "/2+1");
                Assert.AreEqual(0, IntegerTheory.CeilingToPowerOfTwo(-x), "-" + x);
            }

            const long maxPowerOfTwo = 0x4000000000000000;

            Assert.AreEqual(maxPowerOfTwo, IntegerTheory.CeilingToPowerOfTwo(maxPowerOfTwo), "max");
            Assert.AreEqual(maxPowerOfTwo, IntegerTheory.CeilingToPowerOfTwo(maxPowerOfTwo - 1), "max");
        }
        public void CeilingToPowerOfHandlesPositiveIntegersCorrectly32()
        {
            Assert.AreEqual(0, IntegerTheory.CeilingToPowerOfTwo(0), "0");
            Assert.AreEqual(1, IntegerTheory.CeilingToPowerOfTwo(1), "1");
            Assert.AreEqual(2, IntegerTheory.CeilingToPowerOfTwo(2), "2");
            Assert.AreEqual(4, IntegerTheory.CeilingToPowerOfTwo(3), "3");
            Assert.AreEqual(4, IntegerTheory.CeilingToPowerOfTwo(4), "4");

            for (int i = 2; i < 31; i++)
            {
                int x = 1 << i;
                Assert.AreEqual(x, IntegerTheory.CeilingToPowerOfTwo(x), x.ToString());
                Assert.AreEqual(x, IntegerTheory.CeilingToPowerOfTwo(x - 1), x + "-1");
                Assert.AreEqual(x, IntegerTheory.CeilingToPowerOfTwo((x >> 1) + 1), x + "/2+1");
                Assert.AreEqual(0, IntegerTheory.CeilingToPowerOfTwo(-x), "-" + x);
            }

            const int maxPowerOfTwo = 0x40000000;

            Assert.AreEqual(maxPowerOfTwo, IntegerTheory.CeilingToPowerOfTwo(maxPowerOfTwo), "max");
            Assert.AreEqual(maxPowerOfTwo, IntegerTheory.CeilingToPowerOfTwo(maxPowerOfTwo - 1), "max");
        }
        public void PowerOfTwoThrowsWhenOutOfRange32()
        {
            Assert.Throws(
                typeof(ArgumentOutOfRangeException),
                () => IntegerTheory.PowerOfTwo(-1));

            Assert.Throws(
                typeof(ArgumentOutOfRangeException),
                () => IntegerTheory.PowerOfTwo(31));

            Assert.Throws(
                typeof(ArgumentOutOfRangeException),
                () => IntegerTheory.PowerOfTwo(Int32.MinValue));

            Assert.Throws(
                typeof(ArgumentOutOfRangeException),
                () => IntegerTheory.PowerOfTwo(Int32.MaxValue));

            Assert.DoesNotThrow(
                () => IntegerTheory.PowerOfTwo(30));

            Assert.DoesNotThrow(
                () => IntegerTheory.PowerOfTwo(0));
        }
        public void PowerOfTwoThrowsWhenOutOfRange64()
        {
            Assert.Throws(
                typeof(ArgumentOutOfRangeException),
                () => IntegerTheory.PowerOfTwo((long)-1));

            Assert.Throws(
                typeof(ArgumentOutOfRangeException),
                () => IntegerTheory.PowerOfTwo((long)63));

            Assert.Throws(
                typeof(ArgumentOutOfRangeException),
                () => IntegerTheory.PowerOfTwo(Int64.MinValue));

            Assert.Throws(
                typeof(ArgumentOutOfRangeException),
                () => IntegerTheory.PowerOfTwo(Int64.MaxValue));

            Assert.DoesNotThrow(
                () => IntegerTheory.PowerOfTwo((long)62));

            Assert.DoesNotThrow(
                () => IntegerTheory.PowerOfTwo((long)0));
        }