Exemplo n.º 1
0
        /// <summary>
        /// Calculates GCD of values by specified algorithm
        /// </summary>
        /// <param name="algorithm">delegate to method represented the algorithm</param>
        /// <param name="values"></param>
        /// <returns>GCD of values</returns>
        public static int Gcd(GcdAlgorithm algorithm, params int[] values)
        {
            if (algorithm == null)
            {
                throw new ArgumentNullException(nameof(algorithm));
            }

            if (values.Length == 0)
            {
                throw new ArgumentException("Array is empty");
            }

            int gcd = values[0];

            for (int i = 1; i < values.Length; i++)
            {
                gcd = algorithm(values[i], gcd);
                if (gcd == 1)
                {
                    return(gcd);
                }
            }

            return(gcd);
        }
        public int TestGcdWithTime(GcdAlgorithm algorithm, int a, int b)
        {
            ReturnGcdAndCalutationTime result = GcdCalculator.GcdWithTimeCalculation(algorithm, a, b);

            Debug.WriteLine($"Algorithm {algorithm.Method.Name} with parameters {a} and {b} calculates during {result.Time.Ticks} ticks");
            return(result.Gcd);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Calculates GCD of two values by specified algorithm
        /// </summary>
        /// <param name="algorithm">delegate to method represented the algorithm</param>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns>GCD of values</returns>
        public static int Gcd(GcdAlgorithm algorithm, int a, int b)
        {
            if (algorithm == null)
            {
                throw new ArgumentNullException(nameof(algorithm));
            }

            return(algorithm(a, b));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Calculates GCD of values by specified algorithm
        /// </summary>
        /// <param name="algorithm">delegate to method represented the algorithm</param>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns>struct <see cref="ReturnGcdAndCalutationTime"/></returns>
        public static ReturnGcdAndCalutationTime GcdWithTimeCalculation(GcdAlgorithm algorithm, int a, int b)
        {
            if (algorithm == null)
            {
                throw new ArgumentNullException(nameof(algorithm));
            }

            ReturnGcdAndCalutationTime result = new ReturnGcdAndCalutationTime();

            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();
            result.Gcd = algorithm(a, b);
            stopWatch.Stop();
            result.Time = stopWatch.Elapsed;

            return(result);
        }
        private static (int gcd, long runTime) ProcessArray(int[] numbers, GcdAlgorithm algorithmType)
        {
            if (numbers.Length < 1)
            {
                throw new ArgumentException();
            }

            Stopwatch sw        = Stopwatch.StartNew();
            long      startTime = sw.ElapsedMilliseconds;
            int       gcd       = Math.Abs(numbers[0]);

            for (int i = 1; i <= numbers.Length - 1; i++)
            {
                gcd = algorithmType.Invoke(Math.Abs(numbers[i]), gcd);
            }

            return(gcd, sw.ElapsedMilliseconds - startTime);
        }
        public ActionResult Algorithm(int[] numbers, string algorithmType)
        {
            var gcdAlgorithm = new GcdAlgorithm
            {
                Name = algorithmType
            };

            try
            {
                gcdAlgorithm.Gcd = GetGcd(numbers, algorithmType);
            }
            catch (ArgumentException)
            {
                return(RedirectToAction("CalculationError"));
            }
            gcdAlgorithm.InputNumbers = numbers;
            return(View(gcdAlgorithm));
        }
 public void TestGcdParamsThrows(GcdAlgorithm algorithm, int[] values)
 {
     Assert.That(() => GcdCalculator.Gcd(algorithm, values), Throws.TypeOf <ArgumentNullException>());
 }
 public int TestGcdParams(GcdAlgorithm algorithm, int[] values)
 {
     return(GcdCalculator.Gcd(algorithm, values));
 }
 public void TestGcdWithTimeThrows(GcdAlgorithm algorithm, int a, int b)
 {
     Assert.That(() => GcdCalculator.GcdWithTimeCalculation(algorithm, a, b), Throws.TypeOf <ArgumentNullException>());
 }
Exemplo n.º 10
0
        public void EuclidTest()
        {
            int m = 60, n = 24;

            Assert.Equal(12, GcdAlgorithm.Euclid(m, n));
        }