Using System;
|
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace FindPrimeNumbersParallel
|
|
{
|
|
class MainClass
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
var sw = new Stopwatch();
|
|
sw.Start();
|
|
const int numParts = 10;
|
|
var primes = new List[numParts];
|
|
Parallel.For(0, numParts, i => primes[i] = GetPrimeNumbers(i == 0 ? 2 : i * 1000000 + 1, (i + 1) * 1000000));
|
|
var result = primes.Sum(p => p.Count);
|
|
Console.WriteLine("Total prime numbers: {0}\nProcess time: {1}", result, sw.ElapsedMilliseconds);
|
|
}
|
|
|
|
private static List GetPrimeNumbers(int minimum, int maximum)
|
|
{
|
|
List result = new List();
|
|
for (int i = minimum; i <= maximum; i++)
|
|
{
|
|
if (IsPrimeNumber(i))
|
|
{
|
|
result.Add(i);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
static bool IsPrimeNumber(int number)
|
|
{
|
|
if (number % 2 == 0)
|
|
{
|
|
return number == 2;
|
|
}
|
|
else
|
|
{
|
|
var topLimit = (int)Math.Sqrt(number);
|
|
for (int i = 3; i <= topLimit; i += 2)
|
|
{
|
|
if (number % i == 0) return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|