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 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)");
 }
Example #3
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)");
 }
Example #4
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();
        }
        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)");
        }
Example #6
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)");
        }
 public void ListLcmChecksForNullArguments()
 {
     Assert.Throws(
         typeof(ArgumentNullException),
         () => IntegerTheory.LeastCommonMultiple((long[])null));
 }
 public void ListLcmHandlesSpecialInputCorrectly()
 {
     Assert.AreEqual(1, IntegerTheory.LeastCommonMultiple(new long[0]), "Lcm()");
     Assert.AreEqual(100, IntegerTheory.LeastCommonMultiple(-100), "Lcm(-100)");
 }
 public void LcmHandlesNegativeInputCorrectly()
 {
     Assert.AreEqual(352, IntegerTheory.LeastCommonMultiple(11, -32), "Lcm(11,-32)");
     Assert.AreEqual(352, IntegerTheory.LeastCommonMultiple(-11, 32), "Lcm(-11,32)");
     Assert.AreEqual(352, IntegerTheory.LeastCommonMultiple(-11, -32), "Lcm(-11,-32)");
 }