public void ListLcmChecksForNullArguments() { Assert.Throws( typeof(ArgumentNullException), () => Euclid.LeastCommonMultiple((long[])null)); }
public void LcmHandlesNegativeInputCorrectly() { Assert.AreEqual(352, Euclid.LeastCommonMultiple(11, -32), "Lcm(11,-32)"); Assert.AreEqual(352, Euclid.LeastCommonMultiple(-11, 32), "Lcm(-11,32)"); Assert.AreEqual(352, Euclid.LeastCommonMultiple(-11, -32), "Lcm(-11,-32)"); }
public void ListLcmHandlesSpecialInputCorrectly() { Assert.AreEqual(1, Euclid.LeastCommonMultiple(new long[0]), "Lcm()"); Assert.AreEqual(100, Euclid.LeastCommonMultiple(-100), "Lcm(-100)"); }
/// <summary> /// Computes the canonical modulus, where the result has the sign of the divisor, /// for each element of the vector for the given divisor. /// </summary> /// <param name="divisor">The scalar denominator to use.</param> /// <param name="result">A vector to store the results in.</param> protected override void DoModulus(double divisor, Vector <double> result) { Map(x => Euclid.Modulus(x, divisor), result, Zeros.Include); }
public void TestInvalid3() { Euclid.FindGreatestCommonDivisor(-1, -4); }
/// <summary> /// Computes the canonical modulus, where the result has the sign of the divisor, /// for the given divisor each element of the matrix. /// </summary> /// <param name="divisor">The scalar denominator to use.</param> /// <param name="result">Matrix to store the results in.</param> protected override void DoModulus(float divisor, Matrix <float> result) { Map(x => Euclid.Modulus(x, divisor), result, Zeros.Include); }
/// <summary> /// Computes the remainder (% operator), where the result has the sign of the dividend, /// for the given divisor each element of the matrix. /// </summary> /// <param name="divisor">The scalar denominator to use.</param> /// <param name="result">Matrix to store the results in.</param> protected override void DoRemainder(double divisor, Matrix <double> result) { Map(x => Euclid.Remainder(x, divisor), result, Zeros.Include); }
private static void PartTwo(string line) { var cups = line.Select(t => int.Parse(t.ToString())).ToList(); var cupsMax = cups.Max(); var lookup = new Dictionary <int, LinkedListNode <int> >(); lookup.Add(cups[0], new LinkedListNode <int> { Value = cups[0] }); var list = lookup[cups[0]]; var currentCup = list; for (var i = 1; i < cups.Count; i++) { lookup.Add(cups[i], new LinkedListNode <int> { Value = cups[i] }); currentCup.Next = lookup[cups[i]]; currentCup = currentCup.Next; } for (var i = cupsMax + 1; i < 1_000_000; i++) { lookup.Add(i, new LinkedListNode <int> { Value = i }); currentCup.Next = lookup[i]; currentCup = currentCup.Next; } currentCup.Next = list; currentCup = currentCup.Next; for (int i = 0; i < 10_000_000; i++) { var startingCupToRemove = currentCup.Next; var excludedValues = new List <int> { startingCupToRemove.Value, startingCupToRemove.Next.Value, startingCupToRemove.Next.Next.Value }; currentCup.Next = startingCupToRemove.Next.Next.Next; var nextFound = false; var targetCupValue = currentCup.Value - 1; while (!nextFound) { targetCupValue = Euclid.Modulus(targetCupValue, cupsMax); if (targetCupValue == 0) { targetCupValue = cupsMax; } if (!excludedValues.Contains(targetCupValue)) { nextFound = true; } else { targetCupValue--; } } var targetNode = lookup[targetCupValue]; startingCupToRemove.Next.Next.Next = targetNode.Next; targetNode.Next = startingCupToRemove; currentCup = currentCup.Next; } var start = lookup[1]; long result = (long)start.Next.Value * start.Next.Next.Value; Console.WriteLine($"Solution 2: {start.Next.Value} * {start.Next.Next.Value}"); // 776176127776 is too high Console.WriteLine($"Solution 2: {result}"); }
public void ListGcdHandlesSpecialInputCorrectly() { Assert.AreEqual((BigInteger)0, Euclid.GreatestCommonDivisor(new BigInteger[0]), "Gcd()"); Assert.AreEqual((BigInteger)100, Euclid.GreatestCommonDivisor((BigInteger)(-100)), "Gcd(-100)"); }
/// <summary> /// Computes the remainder (% operator), where the result has the sign of the dividend, /// for each element of the vector for the given divisor. /// </summary> /// <param name="divisor">The scalar denominator to use.</param> /// <param name="result">A vector to store the results in.</param> protected override void DoRemainder(float divisor, Vector <float> result) { Map(x => Euclid.Remainder(x, divisor), result, Zeros.Include); }
/// <summary> /// Computes the remainder (% operator), where the result has the sign of the dividend, /// for the given dividend for each element of the vector. /// </summary> /// <param name="dividend">The scalar numerator to use.</param> /// <param name="result">A vector to store the results in.</param> protected override void DoRemainderByThis(float dividend, Vector <float> result) { Map(x => Euclid.Remainder(dividend, x), result, Zeros.Include); }
public void ListLcmHandlesSpecialInputCorrectly() { Assert.AreEqual(1, Euclid.LeastCommonMultiple(Array.Empty <long>()), "Lcm()"); Assert.AreEqual(100, Euclid.LeastCommonMultiple(-100), "Lcm(-100)"); }
/// <summary> /// The internal core method for calculating the autocorrelation. /// </summary> /// <param name="x">The data array to calculate auto correlation for</param> /// <param name="k_low">Min lag to calculate ACF for (0 = no shift with acf=1) must be zero or positive and smaller than x.Length</param> /// <param name="k_high">Max lag (EXCLUSIVE) to calculate ACF for must be positive and smaller than x.Length</param> /// <returns>An array with the ACF as a function of the lags k.</returns> private static double[] AutoCorrelationFft(IEnumerable <double> x, int k_low, int k_high) { if (x == null) { throw new ArgumentNullException(nameof(x)); } int N = x.Count(); // Sample size if (k_low < 0 || k_low >= N) { throw new ArgumentOutOfRangeException(nameof(k_low), "kMin must be zero or positive and smaller than x.Length"); } if (k_high < 0 || k_high >= N) { throw new ArgumentOutOfRangeException(nameof(k_high), "kMax must be positive and smaller than x.Length"); } if (N < 1) { return(new double[0]); } int nFFT = Euclid.CeilingToPowerOfTwo(N) * 2; Complex[] x_fft = new Complex[nFFT]; Complex[] x_fft2 = new Complex[nFFT]; double x_dash = Statistics.Mean(x); double xArrNow = 0.0d; using (IEnumerator <double> iex = x.GetEnumerator()) { for (int ii = 0; ii < nFFT; ii++) { if (ii < N) { if (!iex.MoveNext()) { throw new ArgumentOutOfRangeException(nameof(x)); } xArrNow = iex.Current; x_fft[ii] = new Complex(xArrNow - x_dash, 0.0); // copy values in range and substract mean } else { x_fft[ii] = new Complex(0.0, 0.0); // pad all remaining points } } } Fourier.Forward(x_fft, FourierOptions.Matlab); // maybe a Vector<Complex> implementation here would be faster for (int ii = 0; ii < x_fft.Length; ii++) { x_fft2[ii] = Complex.Multiply(x_fft[ii], Complex.Conjugate(x_fft[ii])); } Fourier.Inverse(x_fft2, FourierOptions.Matlab); double acf_Val1 = x_fft2[0].Real; double[] acf_Vec = new double[k_high - k_low + 1]; // normalize such that acf[0] would be 1.0 for (int ii = 0; ii < (k_high - k_low + 1); ii++) { acf_Vec[ii] = x_fft2[k_low + ii].Real / acf_Val1; } return(acf_Vec); }
/// <summary> /// Executes the example. /// </summary> public override void ExecuteExample() { // 1. Find out whether the provided number is an even number MathDisplay.WriteLine(@"1. Find out whether the provided number is an even number"); MathDisplay.WriteLine(@"{0} is even = {1}. {2} is even = {3}", 1, Euclid.IsEven(1), 2, 2.IsEven()); MathDisplay.WriteLine(); // 2. Find out whether the provided number is an odd number MathDisplay.WriteLine(@"2. Find out whether the provided number is an odd number"); MathDisplay.WriteLine(@"{0} is odd = {1}. {2} is odd = {3}", 1, 1.IsOdd(), 2, Euclid.IsOdd(2)); MathDisplay.WriteLine(); // 3. Find out whether the provided number is a perfect power of two MathDisplay.WriteLine(@"2. Find out whether the provided number is a perfect power of two"); MathDisplay.WriteLine( @"{0} is power of two = {1}. {2} is power of two = {3}", 5, 5.IsPowerOfTwo(), 16, Euclid.IsPowerOfTwo(16)); MathDisplay.WriteLine(); // 4. Find the closest perfect power of two that is larger or equal to 97 MathDisplay.WriteLine(@"4. Find the closest perfect power of two that is larger or equal to 97"); MathDisplay.WriteLine(97.CeilingToPowerOfTwo().ToString()); MathDisplay.WriteLine(); // 5. Raise 2 to the 16 MathDisplay.WriteLine(@"5. Raise 2 to the 16"); MathDisplay.WriteLine(16.PowerOfTwo().ToString()); MathDisplay.WriteLine(); // 6. Find out whether the number is a perfect square MathDisplay.WriteLine(@"6. Find out whether the number is a perfect square"); MathDisplay.WriteLine( @"{0} is perfect square = {1}. {2} is perfect square = {3}", 37, 37.IsPerfectSquare(), 81, Euclid.IsPerfectSquare(81)); MathDisplay.WriteLine(); // 7. Compute the greatest common divisor of 32 and 36 MathDisplay.WriteLine(@"7. Returns the greatest common divisor of 32 and 36"); MathDisplay.WriteLine(Euclid.GreatestCommonDivisor(32, 36).ToString()); MathDisplay.WriteLine(); // 8. Compute the greatest common divisor of 492, -984, 123, 246 MathDisplay.WriteLine(@"8. Returns the greatest common divisor of 492, -984, 123, 246"); MathDisplay.WriteLine(Euclid.GreatestCommonDivisor(492, -984, 123, 246).ToString()); MathDisplay.WriteLine(); // 9. Compute the extended greatest common divisor "z", such that 45*x + 18*y = z MathDisplay.WriteLine(@"9. Compute the extended greatest common divisor Z, such that 45*x + 18*y = Z"); long x, y; var z = Euclid.ExtendedGreatestCommonDivisor(45, 18, out x, out y); MathDisplay.WriteLine(@"z = {0}, x = {1}, y = {2}. 45*{1} + 18*{2} = {0}", z, x, y); MathDisplay.WriteLine(); // 10. Compute the least common multiple of 16 and 12 MathDisplay.WriteLine(@"10. Compute the least common multiple of 16 and 12"); MathDisplay.WriteLine(Euclid.LeastCommonMultiple(16, 12).ToString()); MathDisplay.WriteLine(); }
/// <summary> /// Calculates the amount of degrees closest to zero to add to an angle to get a certain other angle. /// </summary> /// <param name="source">The angle in degrees to add the result of this method to.</param> /// <param name="destination">The target angle in degrees.</param> /// <returns>The amount of degrees closest to zero to add to the source angle to get the destination angle.</returns> public static double SmallesAngle(float source, float destination) { double a = destination - source; return(Euclid.Modulus(a + 180, 360) - 180); }
public void LcmSupportsLargeInput() { Assert.AreEqual((BigInteger)Int32.MaxValue, Euclid.LeastCommonMultiple((BigInteger)Int32.MaxValue, Int32.MaxValue), "Lcm(Int32Max,Int32Max)"); Assert.AreEqual((BigInteger)Int64.MaxValue, Euclid.LeastCommonMultiple((BigInteger)Int64.MaxValue, Int64.MaxValue), "Lcm(Int64Max,Int64Max)"); Assert.AreEqual((BigInteger)Int64.MaxValue, Euclid.LeastCommonMultiple((BigInteger)(-Int64.MaxValue), -Int64.MaxValue), "Lcm(-Int64Max,-Int64Max)"); Assert.AreEqual((BigInteger)Int64.MaxValue, Euclid.LeastCommonMultiple((BigInteger)(-Int64.MaxValue), Int64.MaxValue), "Lcm(-Int64Max,Int64Max)"); #if !PORTABLE Assert.AreEqual(BigInteger.Parse("91739176367857263082719902034485224119528064014300888465614024"), Euclid.LeastCommonMultiple(BigInteger.Parse("7305316061155559483748611586449542122662"), BigInteger.Parse("57377277362010117405715236427413896")), "Lcm(large)"); #endif }
/// <summary> /// Calculates the summation of multiple angles. /// </summary> /// <param name="angles">The angles in degrees to calculate the summation of.</param> /// <returns>The summation between 0 and 360 degrees.</returns> public static float Sum(params float[] angles) { return(Euclid.Modulus(angles.Sum(), 360)); }
public void ListLcmHandlesSpecialInputCorrectly() { Assert.AreEqual((BigInteger)1, Euclid.LeastCommonMultiple(new BigInteger[0]), "Lcm()"); Assert.AreEqual((BigInteger)100, Euclid.LeastCommonMultiple((BigInteger)(-100)), "Lcm(-100)"); }
/// <summary> /// Computes the canonical modulus, where the result has the sign of the divisor, /// for the given dividend for each element of the matrix. /// </summary> /// <param name="dividend">The scalar numerator to use.</param> /// <param name="result">A vector to store the results in.</param> protected override void DoModulusByThis(float dividend, Matrix <float> result) { Map(x => Euclid.Modulus(dividend, x), result, Zeros.Include); }
public void ListGcdHandlesSpecialInputCorrectly() { Assert.AreEqual(0, Euclid.GreatestCommonDivisor(new long[0]), "Gcd()"); Assert.AreEqual(100, Euclid.GreatestCommonDivisor(-100), "Gcd(-100)"); }
/// <summary> /// Computes the remainder (% operator), where the result has the sign of the dividend, /// for the given dividend for each element of the matrix. /// </summary> /// <param name="dividend">The scalar numerator to use.</param> /// <param name="result">A vector to store the results in.</param> protected override void DoRemainderByThis(double dividend, Matrix <double> result) { Map(x => Euclid.Remainder(dividend, x), result, Zeros.Include); }
public void ListGcdChecksForNullArguments() { Assert.Throws( typeof(ArgumentNullException), () => Euclid.GreatestCommonDivisor((long[])null)); }
/// <summary> /// Computes the canonical modulus, where the result has the sign of the divisor, /// for the given dividend for each element of the vector. /// </summary> /// <param name="dividend">The scalar numerator to use.</param> /// <param name="result">A vector to store the results in.</param> protected override void DoModulusByThis(double dividend, Vector <double> result) { Map(x => Euclid.Modulus(dividend, x), result, Zeros.Include); }
public void TestInvalid2() { Euclid.FindGreatestCommonDivisor(8, -1); }