/// <summary> /// Finds gcd of the selected method /// </summary> /// <param name="wholeTime">Working time</param> /// <param name="method">Method for finding gcd</param> /// <param name="numbers">List of numbers for finding gcd</param> /// <returns>GCD</returns> public static int FindGCD(out long wholeTime, GCDFindingMethod method, params int[] numbers) { if (numbers == null) throw new ArgumentNullException("Array is null"); if (method == null) throw new ArgumentNullException("Method for finding gcd is not defined"); if (numbers.Length == 0 || numbers.Length == 1) throw new ArgumentException("Less than two numbers"); int result = numbers[0]; wholeTime = 0; Stopwatch timer = new Stopwatch(); timer.Start(); for (int i = 0; i < numbers.Length - 1; i++) { if (result == 1) break; result = method(result, numbers[i + 1]); } timer.Stop(); wholeTime = timer.ElapsedTicks; return result; }
/// <summary> /// Finds gcd with selected method /// </summary> /// <param name="first">First number</param> /// <param name="second">Second number</param> /// <param name="wholeTime">Algorithm working time</param> /// <param name="method">Method for finding gcd</param> /// <returns>GCD of two numbers</returns> public static int FindGCD(int first, int second, out long wholeTime, GCDFindingMethod method) { if (method == null) throw new ArgumentNullException("Method for finding gcd is not defined"); wholeTime = 0; Stopwatch timer = new Stopwatch(); int result = 0; timer.Start(); result = method(first, second); timer.Stop(); wholeTime = timer.ElapsedTicks; return result; }