public static int CalculateGCD(GCDAlgorithm method, int a, int b)
 {
     GCDAlgorithm algorithm = method;
     if (algorithm == null)
         throw new ArgumentNullException("Parametr 'method' is null");
     return algorithm(a, b);
 }
예제 #2
0
 private static long Calculate(GCDAlgorithm algorithm, out long ticks, params long[] array) {
     if (array == null)
         throw new ArgumentNullException(nameof(array));
     long result = 0;
     ticks = 0;
     foreach (long element in array) {
         long t;
         result = Calculate(result, element, out t, algorithm);
         ticks += t;
     }
     return result;
 }
        public static int CalculateGCDWithExecutionTime(out long executionTime, GCDAlgorithm method, int a, int b)
        {
            GCDAlgorithm algorithm = method;
            if (algorithm == null)
                throw new ArgumentNullException("Parametr 'method' is null");

            Stopwatch watch = new Stopwatch();
            watch.Start();
            int result = algorithm(a, b);
            watch.Stop();
            executionTime = watch.ElapsedTicks;
            return result;
        }
        public static int CalculateGCDWithExecutionTime(out long executionTime, GCDAlgorithm method, params int[] numbers)
        {
            GCDAlgorithm algorithm = method;
            if (algorithm == null)
                throw new ArgumentNullException("Parametr 'method' is null");

            Stopwatch timeWork = new Stopwatch();
            timeWork.Start();
            int result = CalculateGCD(method, numbers);
            timeWork.Stop();
            executionTime = timeWork.ElapsedTicks;
            return result;
        }
        public static int CalculateGCD(GCDAlgorithm method, params int[] numbers)
        {
            if (numbers == null)
                throw new ArgumentException("parametr 'numbers' is null");
            GCDAlgorithm algorithm = method;
            if (algorithm == null)
                throw new ArgumentNullException("Parametr 'method' is null");

            int result = 0;
            for (int i = 0; i < numbers.Length; i++)
                result = algorithm(result, numbers[i]);

            return Math.Abs(result);
        }
예제 #6
0
        private static long GCD(long[] _numbers, GCDAlgorithm _alg)
        {
            int  n   = _numbers.Length;
            long gcd = _numbers[0];

            if (n == 0)
            {
                return(0);
            }
            for (int i = 0; i < n - 1; i++)
            {
                gcd = _alg.Invoke(Math.Abs(gcd), Math.Abs(_numbers[i + 1]));
            }
            return(gcd);
        }
예제 #7
0
        /// <summary>Computes GCD for unlimited number of parameters for Stein's and Euclidean algorithms.</summary>
        /// <remarks>Uses SteinsAlgorithm() with one, two, three arguments.</remarks>
        /// <param name="gcd"> Delegate type instance.</param>
        /// <param name="numbers"> The range of numbers GCD is computing for.</param>
        /// <returns>Returns an absolute integer value of GCD
        /// or 0 if all arguments equals 0</returns>
        /// <seealso cref="SteinsAlgorithm(int, int)"/>
        /// <seealso cref="EuclideanAlgorithm(int, int)"/>
        private static int ParamsGCDMethod(GCDAlgorithm gcd, params int[] numbers)
        {
            if (numbers == null || numbers.Length == 0)
            {
                throw new ArgumentNullException(nameof(numbers));
            }

            if (numbers.Length <= 3)
            {
                if (numbers.Length == 3)
                {
                    return(ParamsGCDMethod(gcd, numbers[0], numbers[1], numbers[2]));
                }
                else if (numbers.Length == 2)
                {
                    return(ParamsGCDMethod(gcd, numbers[0], numbers[1]));
                }
                else
                {
                    return(ParamsGCDMethod(gcd, numbers[0]));
                }
            }
            else
            {
                int[] left  = new int[numbers.Length / 2];
                int[] right = new int[numbers.Length - (numbers.Length / 2)];
                for (int i = 0; i < numbers.Length; i++)
                {
                    if (i < numbers.Length / 2)
                    {
                        left[i] = numbers[i];
                    }
                    else
                    {
                        right[i - (numbers.Length / 2)] = numbers[i];
                    }
                }

                int gcd_left  = ParamsGCDMethod(gcd, left);
                int gcd_right = ParamsGCDMethod(gcd, right);

                return(ParamsGCDMethod(gcd, gcd_left, gcd_right));
            }
        }
예제 #8
0
        public void GCD_StopWatchTest(int first, int second)
        {
            Stopwatch sw = new Stopwatch();


            sw.Start();
            GCDAlgorithm.FindGCDEuclid(first, second);
            sw.Stop();
            var euclid = sw.Elapsed;

            sw.Reset();

            sw.Start();
            GCDAlgorithm.FindGCDStein(first, second);
            sw.Stop();
            var shtein = sw.Elapsed;

            Debug.WriteLine($"euclid: {euclid}, shtein: {shtein}");
            Assert.AreNotSame(euclid, shtein);
        }
예제 #9
0
        /// <returns>Greatest common divisor of an array.</returns>
        /// <summary>
        /// Universal GCD calculator for array.
        /// </summary>
        /// <param name="algGCD">GCD Algorithm</param>
        /// <param name="swMiliSec">Tike taken (Stopwatch calculation) in milliseconds.</param>
        /// <param name="dtMiliSec">Tike taken (DateTime calculation) in milliseconds.</param>
        /// <param name="arr">Array to analyze</param>
        /// <returns></returns>
        public static int GCDArrayCalculator(GCDAlgorithm algGCD, out int stopTatchMs, out int dateTimeMs, params int[] arr)
        {
            _ = algGCD ?? throw new ArgumentNullException(nameof(algGCD), "Algorithm should not be null");
            _ = arr ?? throw new ArgumentNullException(nameof(arr), "Numbers array should not be null");

            //// Deleting all zeroes.
            var arrToAnalyze = arr.Where(val => val != 0).ToArray();

            if (arrToAnalyze.Length < 2)
            {
                throw new ArgumentException("Less than 2 numbers provided. Zeroes are not accepted.", nameof(arr));
            }

            //// Time taken calculation. Start.
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            DateTime start = DateTime.Now;

            //// Calculation algorithm call.
            var result = algGCD(arrToAnalyze[0], arrToAnalyze[1]);

            for (int i = 2; i < arrToAnalyze.Length; i++)
            {
                result = algGCD(result, arrToAnalyze[i]);
            }

            //// Time taken calculation. Stop.
            stopwatch.Stop();
            TimeSpan stopwatTimeSpan = stopwatch.Elapsed;

            stopTatchMs = Convert.ToInt32(stopwatTimeSpan.TotalMilliseconds);

            DateTime end          = DateTime.Now;
            TimeSpan dateTimeSpan = end - start;

            dateTimeMs = Convert.ToInt32(dateTimeSpan.TotalMilliseconds);

            return(result);
        }
예제 #10
0
        private static long Calculate(long a, long b, GCDAlgorithm algorithm) {
            if (a < 0)
                a *= -1;
            if (b < 0)
                b *= -1;
            if (a == b)
                return b;
            if (a == 0)
                return b;

            return b == 0 ? a : algorithm(a, b);
        }
예제 #11
0
 public void FindGCD_SteinClassObject_ValidValue(int[] array)
 {
     Assert.AreEqual(GCDAlgorithm.FindGCDStein(array), GCDAlgorithm.FindGCD(new SteinGCD(), array));
 }
예제 #12
0
 private static long Calculate(GCDAlgorithm algorithm, params long[] array) {
     if (array == null)
         throw new ArgumentNullException(nameof(array));
     return array.Aggregate<long, long>(0, (a, b) => Calculate(a, b, algorithm));
 }
예제 #13
0
 private static long Calculate(long a, long b, out long ticks, GCDAlgorithm algorithm) {
     Stopwatch timer = new Stopwatch();
     timer.Start();
     long result = Calculate(a, b, algorithm);
     timer.Stop();
     ticks = timer.ElapsedTicks;
     return result;
 }
예제 #14
0
        public void GCDEuclid_TenAndFive_Five(int first, int second, int expected)
        {
            var actual = GCDAlgorithm.FindGCDEuclid(first, second);

            Assert.AreEqual(expected, actual);
        }
예제 #15
0
        public void FindGCD_SteinDelegate_ValidOutPut(int[] array)
        {
            Func <int, int, int> delegateGcd = new SteinGCD().FindGcd;

            Assert.AreEqual(GCDAlgorithm.FindGCDStein(array), GCDAlgorithm.FindGCD(delegateGcd, array));
        }
예제 #16
0
 public void FindGCD_EuclidClassObject_ValidValue(int[] array)
 {
     Assert.AreEqual(GCDAlgorithm.FindGCDEuclid(array), GCDAlgorithm.FindGCD(new EuclidGCD(), array));
 }
예제 #17
0
 public void GCDEuclid_ParamsInput_ValidOutPut(int[] intValues, int expected)
 {
     Assert.AreEqual(expected, GCDAlgorithm.FindGCDEuclid(intValues));
 }
예제 #18
0
 public void GetShteinGCD_ArgumentNullExceptionTests()
 {
     Assert.Throws <ArgumentNullException>(() => GCDAlgorithm.GetSteinGCD(null));
 }
예제 #19
0
 public void GetShteinGCD_ArgumentExceptionTests(params int[] numbersArray)
 {
     Assert.Throws <ArgumentException>(() => GCDAlgorithm.GetSteinGCD(numbersArray));
 }
예제 #20
0
 public int GetShteinGCD_GetNumbersArrayGCGTests(params int[] numbersArray)
 {
     return(GCDAlgorithm.GetSteinGCD(numbersArray));
 }