/// <summary> /// Represents the Binary algorithm to find GCD of values. /// </summary> /// <param name="timeMilliseconds">The execution time.</param> /// <param name="array">The array of values.</param> /// <returns>The greatest common divisor.</returns> public static int Binary(out long timeMilliseconds, params int[] array) { Stopwatch watch = Stopwatch.StartNew(); LogicGCD logic = new LogicGCD(LogicBinary); int result = HiddenGCD(array, logic); watch.Stop(); timeMilliseconds = watch.ElapsedMilliseconds; return(result); }
/// <summary> /// Main method to calculate gcd. /// </summary> /// <param name="array">The array of values.</param> /// <param name="logic">Pointer to gcd method of two parameters.</param> /// <returns>The <see cref="int"/>.</returns> private static int HiddenGCD(int[] array, LogicGCD logic) { if (array == null) { throw new ArgumentNullException(nameof(array)); } if (array.Length == 0) { throw new ArgumentException(nameof(array)); } int result = array[0]; for (int i = 1; i < array.Length; i++) { result = logic(result, array[i]); } return(result); }
/// <summary> /// Represents the Euclidean algorithm to find GCD of values. /// </summary> /// <param name="array">The array of values.</param> /// <returns>The greatest common divisor.</returns> public static int Euclidean(params int[] array) { LogicGCD logic = new LogicGCD(LogicEuclidean); return(HiddenGCD(array, logic)); }
/// <summary> /// Represents the Binary algorithm to find GCD of values. /// </summary> /// <param name="array">The array of values.</param> /// <returns>The greatest common divisor.</returns> public static int Binary(params int[] array) { LogicGCD logic = new LogicGCD(LogicBinary); return(HiddenGCD(array, logic)); }