Esempio n. 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);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            var parallelismTimer = new Stopwatch();

            for (int i = 1; i <= 8; i++)
            {
                parallelismTimer.Start();
                PrimeFinder.CalcPrimes(0, 1000000, i);
                Console.WriteLine($"Time in milliseconds for CalcPrimes using {i} max tasks: {parallelismTimer.ElapsedMilliseconds}.");
                parallelismTimer.Reset();
                Console.WriteLine();
            }
        }
Esempio n. 3
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);
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            Console.WriteLine("Prime calculator!\nPlease input first number for your prime number range:");
            int input1 = UserInput();

            Console.WriteLine("Now please input second number for your prime number range:");
            int input2 = UserInput();

            int[] primeArray = PrimeFinder.CalcPrimes(input1, input2);
            if (primeArray.Length == 0)
            {
                Console.WriteLine("No prime numbers in range. Sorry :'(");
            }
            else
            {
                Console.WriteLine("The prime numbers within your given range are:\n");
                foreach (int prime in primeArray)
                {
                    Console.Write(prime + ", ");
                }
            }
        }