Пример #1
0
        /// <summary>
        /// Returns an integer array of all prime numbers between input 1 and input 2 of user.
        /// </summary>
        /// <param name="input1"></param>
        /// <param name="input2"></param>
        /// <param name="degreeOfParallelism"></param>
        /// <returns></returns>
        public static int[] CalcPrimes(int input1, int input2, int degreeOfParallelism)
        {
            var lockerObject   = new object();
            var primesRange    = Enumerable.Range(input1, input2);
            var maxParallelism = new ParallelOptions()
            {
                MaxDegreeOfParallelism = degreeOfParallelism
            };

            var primeList = new ArrayList();

            Parallel.ForEach(primesRange, maxParallelism, (number) =>
            {
                if (PrimeFinder.CheckPrime(number))
                {
                    lock (lockerObject)
                    {
                        primeList.Add(number);
                    }
                }
            });

            var primeArray = new int[primeList.Count];

            primeList.CopyTo(primeArray);
            return(primeArray);
        }
Пример #2
0
        /// <summary>
        /// Returns an integer array of all prime numbers between input 1 and input 2 of user.
        /// </summary>
        /// <param name="input1"></param>
        /// <param name="input2"></param>
        /// <returns></returns>
        public static int[] CalcPrimes(int input1, int input2)
        {
            ArrayList primeList = new ArrayList();

            for (int i = Math.Min(input1, input2); i <= Math.Max(input1, input2); i++)
            {
                if (PrimeFinder.CheckPrime(i))
                {
                    primeList.Add(i);
                }
            }
            int[] primeArray = new int[primeList.Count];
            primeList.CopyTo(primeArray);
            return(primeArray);
        }