/// <summary> Calculate GCD of 2, 3 and more numbers by certain algorithm and show time </summary> /// <param name="numbers">Numbers to get GCD of</param> /// <param name="time">Time of execution</param> /// <param name="method"> GCD algorithm</param> /// <returns>GCD of 2, 3 and more numbers</returns> private static int CalculateGcdWithTime(out long time, GcdMethod method, params int[] numbers) { Stopwatch timer = new Stopwatch(); CalculateGcd(method, numbers); // get result without time wasted on compilation timer.Start(); int result = CalculateGcd(method, numbers); timer.Stop(); time = timer.ElapsedMilliseconds; return(result); }
/// <summary> Calculate GCD of 2, 3 and more numbers by certain algorithm </summary> /// <param name="numbers">Numbers to get GCD of</param> /// <param name="method"> GCD algorithm</param> /// <returns>GCD of 2, 3 and more numbers</returns> private static int CalculateGcd(GcdMethod method, params int[] numbers) { if (numbers == null) { throw new ArgumentNullException(); } if (numbers.Length == 0 || numbers.Length == 1) { throw new ArgumentException(); } int curGcd = method(Math.Abs(numbers[0]), Math.Abs(numbers[1])); for (int i = 2; i < numbers.Length; i++) { curGcd = method(curGcd, Math.Abs(numbers[i])); } return(curGcd); }
/// <summary> /// Calculate GCD of 2, 3 and more numbers by Stein algorithm and show time /// </summary> /// <param name="numbers">Numbers to get GCD of</param> /// <param name="time">Time of execution</param> /// <returns>GCD of 2, 3 and more numbers</returns> public static int SteinGcdWithTime(out long time, params int[] numbers) { GcdMethod method = RecursiveSteinGcd; return(CalculateGcdWithTime(out time, method, numbers)); }
/// <summary> /// Calculate GCD of 2, 3 and more numbers by Stein algorithm /// </summary> /// <param name="numbers">Numbers to get GCD of</param> /// <returns>GCD of 2, 3 and more numbers</returns> public static int SteinGcd(params int[] numbers) { GcdMethod method = RecursiveSteinGcd; return(CalculateGcd(method, numbers)); }
/// <summary> /// Calculate GCD of 2, 3 and more numbers by Euclidean algorithm and show time /// </summary> /// <param name="numbers">Numbers to get GCD of</param> /// <param name="time">Time of execution</param> /// <returns>GCD of 2, 3 and more numbers</returns> public static int EuclideanGcdWithTime(out long time, params int[] numbers) { GcdMethod method = SimpleEuclideanGcd; return(CalculateGcdWithTime(out time, method, numbers)); }
/// <summary> /// Calculate GCD of 2, 3 and more numbers by Euclidean algorithm /// </summary> /// <param name="numbers">Numbers to get GCD of</param> /// <returns>GCD of 2, 3 and more numbers</returns> public static int EuclideanGcd(params int[] numbers) { GcdMethod method = SimpleEuclideanGcd; return(CalculateGcd(method, numbers)); }