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 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)"); }
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 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)"); }
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)"); }
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)"); }
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)"); }
/// <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 ListGcdChecksForNullArguments() { Assert.Throws( typeof(ArgumentNullException), () => IntegerTheory.GreatestCommonDivisor((long[])null)); }
public void ListGcdHandlesSpecialInputCorrectly() { Assert.AreEqual(0, IntegerTheory.GreatestCommonDivisor(new long[0]), "Gcd()"); Assert.AreEqual(100, IntegerTheory.GreatestCommonDivisor(-100), "Gcd(-100)"); }
public void ListGcdHandlesSpecialInputCorrectly() { Assert.AreEqual((BigInteger)0, IntegerTheory.GreatestCommonDivisor(new BigInteger[0]), "Gcd()"); Assert.AreEqual((BigInteger)100, IntegerTheory.GreatestCommonDivisor((BigInteger)(-100)), "Gcd(-100)"); }