-
Notifications
You must be signed in to change notification settings - Fork 0
/
csharp
59 lines (55 loc) · 1.69 KB
/
csharp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System.Collections.Concurrent;
class Program
{
private const int min = 2;
private static int max = 22;
private static readonly ConcurrentQueue<int> primos = new ConcurrentQueue<int>();
// Regra de Definição dos Números Primos
private static void GerarPrimos(int start, int range)
{
var numeroPrimo = true;
var end = start + range;
for (var i = start; i < end; i++)
{
for (var j = min; j < Math.Sqrt(end); j++)
{
if (i != j && i % j == 0)
{
numeroPrimo = false;
break;
}
}
if (numeroPrimo)
{
primos.Enqueue(i);
}
numeroPrimo = true;
}
}
// Regra de Definição da Thread
private static void Main(string[] args)
{
var threadCount = Convert.ToInt32(Console.ReadLine());
var threads = new Thread[threadCount];
var range = (max - min) / threadCount;
var start = min;
for (var i = 0; i < threadCount - 1; i++)
{
var startl = start;
threads[i] = new Thread(() => GerarPrimos(startl, range));
start += range;
threads[i].Start();
}
threads[threadCount - 1] = new Thread(() => GerarPrimos(start, range + (max - min) % threadCount));
threads[threadCount - 1].Start();
for (var i = 0; i < threadCount; i++)
threads[i].Join();
PrintPrimes();
}
// Exibição dos números primos
private static void PrintPrimes()
{
foreach (var i in primos)
Console.WriteLine(i);
}
}