コード例 #1
0
        /// <summary>
        /// Find the GCD of two or more integers
        /// </summary>
        /// <param name="numbers">Integers</param>
        /// <returns>An integer that is the GCD of all numbers</returns>
        /// <exception cref="ArgumentNullException">
        /// Thrown when parameter numbers is null
        /// </exception>
        /// <exception cref="ArgumentException">
        /// Thrown when count of parameters is less than two or when all parameters is zeroes
        /// </exception>
        public static int GetGCDStein(this EuclidAlgorithm algo, params int[] numbers)
        {
            if (numbers == null)
            {
                throw new ArgumentNullException();
            }

            if (numbers.Length < 2)
            {
                throw new ArgumentException("Count of arguments must be two or more");
            }

            int gcd = 0;

            for (int i = 0; i < numbers.Length; i++)
            {
                gcd = GetGCDStein(gcd, numbers[i]);
            }

            if (gcd == 0)
            {
                throw new ArgumentException("At least one argument must be non-zero");
            }

            return(gcd);
        }
コード例 #2
0
        /// <summary>
        /// Find the GCD of two or more integers and method execution time
        /// </summary>
        /// <param name="time">Out parameter time of method execution</param>
        /// <param name="numbers">Integers</param>
        /// <returns>An integer that is the GCD of all numbers</returns>
        /// <exception cref="ArgumentNullException">
        /// Thrown when parameter numbers is null
        /// </exception>
        /// <exception cref="ArgumentException">
        /// Thrown when count of numbers parameters is less than two or when all numbers is zeroes
        /// </exception>
        public static int GetGCDSteinAndTime(this EuclidAlgorithm algo, out double time, params int[] numbers)
        {
            int gcd;
            var wathc = Stopwatch.StartNew();

            gcd = algo.GetGCDStein(numbers);
            wathc.Stop();
            time = wathc.Elapsed.TotalMilliseconds;
            return(gcd);
        }