using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; namespace ConsoleApp3 { class Program { class Parameters { public ulong from; public ulong to; public ulong skip; public ulong[] l; public Mutex m; public Parameters(ulong from, ulong to, ulong skip, ulong[] l, Mutex m) { this.from = from; this.to = to; this.skip = skip; this.l = l; this.m = m; } } static public void skontroluj(object p) { Parameters p2 = p as Parameters; for (ulong i = p2.from; i < p2.to; i += p2.skip) { if (JePrvocislo(i)) { p2.m.WaitOne(); p2.l[p2.l[335]] = i; Thread.Sleep(1); p2.l[335]++; p2.m.ReleaseMutex(); } } } static bool JePrvocislo(UInt64 cislo) { if (cislo % 2 == 0) return false; for (UInt64 i = 3; i * i <= cislo; i+=2) { if (cislo % i == 0) return false; } return true; } static void Main(string[] args) { ulong n = 11; Mutex m = new Mutex(); Thread[] threads = new Thread[n]; ulong[] primes = new ulong[336]; for (ulong i = 0; i < n; i++) { threads[i] = new Thread(skontroluj); threads[i].Start(new Parameters(1000000000000 + (ulong)i, 1000000010000, n, primes,m)); } foreach (Thread t in threads) { t.Join(); } foreach (ulong i in primes) Console.WriteLine(i); Console.WriteLine("Hotovo!"); //skontroluj(1000000000000, 2000000000000, 8); } } }