Exemplo n.º 1
0
        /// <summary>
        /// Method that calculates GCD for any number of integer values.
        /// </summary>
        /// <param name="option">
        /// defines calculation algorithm(Euclidean, Stein).
        /// </param>
        /// <param name="integers">
        /// params int[].
        /// </param>
        /// <returns>
        /// GCD for params int[].
        /// </returns>
        public static int GetGCD(AlgOption option, params int[] integers)
        {
            int gcd = integers[0];

            for (int i = 1; i < integers.Length; i++)
            {
                if (integers[i] == 0 && gcd == 0)
                {
                    continue; // skips iteration if both arguments are zero
                }

                if (option == 0)
                {
                    gcd = GetGCDForTwo(gcd, integers[i]);
                }
                else
                {
                    gcd = GetGCDForTwoBinary(gcd, integers[i]);
                }
            }

            // if all arguments are zero gcd cannot be calculated (infinity?)
            if (gcd == 0)
            {
                throw new ArgumentException(message: "GCD cannot be calculated if all numbers are 0");
            }

            return(gcd);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Overloaded version of method designed to calculate runtime for given algorithm.
        /// </summary>
        /// <param name="option">
        /// defines calculation algorithm(Euclidean, Stein).
        /// </param>
        /// <param name="time">
        /// returns runtime(out).
        /// </param>
        /// <param name="integers">
        /// params int[].
        /// </param>
        /// <returns>
        /// GCD.
        /// </returns>
        public static int GetGCD(AlgOption option, out int time, params int[] integers)
        {
            var watch    = new Stopwatch();
            int firstRun = GetGCDForTwo(187, 319);

            firstRun = GetGCDForTwoBinary(187, 319);

            watch.Start();
            int gcd = 0;

            // algorithms take less than millisecond.
            // in order to calculate runtime it runs algorithm a million times.
            for (int j = 0; j < 1000000; j++)
            {
                gcd = integers[0];
                for (int i = 1; i < integers.Length; i++)
                {
                    if (integers[i] == 0 && gcd == 0)
                    {
                        continue;
                    }

                    if (option == 0)
                    {
                        gcd = GetGCDForTwo(gcd, integers[i]);
                    }
                    else
                    {
                        gcd = GetGCDForTwoBinary(gcd, integers[i]);
                    }
                }

                if (gcd == 0)
                {
                    throw new ArgumentException(message: "GCD cannot be calculated if all numbers are 0");
                }
            }

            watch.Stop();
            time = (int)watch.ElapsedMilliseconds;
            return(gcd);
        }