From 3d2d71d5617774cbf8934cd20cc3cc23613be342 Mon Sep 17 00:00:00 2001 From: Koka Date: Tue, 24 Oct 2017 15:35:11 +0200 Subject: [PATCH 1/2] Added Programm to calculate prime numbers --- prim_number/Program.cs | 83 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 prim_number/Program.cs diff --git a/prim_number/Program.cs b/prim_number/Program.cs new file mode 100644 index 000000000..ce79e7adc --- /dev/null +++ b/prim_number/Program.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Threading.Tasks; + +namespace Program +{ + internal class Program + { + private static void Main(string[] args) + { + Console.WriteLine( + "max Primzahl nr. Primzahlen ein Thread zwei Threads drei Threads vier Threads"); + for (int i = 100000; i <= 51200000; i += 100000) + { + Dictionary times = new Dictionary(); + int anz = 0; + for (int threads = 1; threads <= 4; threads++) + { + Stopwatch watch = new Stopwatch(); + watch.Start(); + anz = new ConcurrentPrimCalculatior().Check(i, threads); + watch.Stop(); + times[threads] = watch.ElapsedMilliseconds; + } + Console.WriteLine($"{i,-15} {anz,-18} {times[1],-15} {times[2],-17} {times[3],-17} {times[4],-12}"); + } + + Console.WriteLine("Done"); + Console.ReadLine(); + } + } + + public class ConcurrentPrimCalculatior + { + public int Check(int maxToCheck, int maxParralell) + { + ConcurrentDictionary validPrims = new ConcurrentDictionary(); + // Create list of valids + for (int i = 2; i < maxToCheck; i++) + validPrims[i] = true; + + int lastToRemove = (int) Math.Sqrt(maxToCheck); + + // Remove everythin before lastToRemove Single-Threaded + int currentPrim = 2; + while (currentPrim <= lastToRemove) + { + int toRemove = currentPrim; + for (int i = 2; i * currentPrim <= lastToRemove; i++) + { + bool useless; + validPrims.TryRemove(i * currentPrim, out useless); + } + + currentPrim = validPrims.Keys.ToList()[validPrims.Keys.ToList().IndexOf(currentPrim) + 1]; + } + + List primsBeforeLastToCheck = new List(); + foreach (int prim in validPrims.Keys) + if (prim > lastToRemove) + break; + else + primsBeforeLastToCheck.Add(prim); + + // Now remove mutiples of 0 - lastToRemove + ParallelOptions options = new ParallelOptions {MaxDegreeOfParallelism = maxParralell}; + Parallel.ForEach(primsBeforeLastToCheck, options, toCheck => + { + for (int mutiiple = toCheck * 2; mutiiple <= maxToCheck; mutiiple += toCheck) + lock (validPrims) + { + bool useless; + validPrims.TryRemove(mutiiple, out useless); + } + }); + + return validPrims.Count; + } + } +} \ No newline at end of file From d9f576e6adad310deb00cddb2a3be42f0bd7696d Mon Sep 17 00:00:00 2001 From: Koka Date: Tue, 24 Oct 2017 15:38:49 +0200 Subject: [PATCH 2/2] Rename Programm.cs --- prim_number/{Program.cs => CSharp.cs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename prim_number/{Program.cs => CSharp.cs} (100%) diff --git a/prim_number/Program.cs b/prim_number/CSharp.cs similarity index 100% rename from prim_number/Program.cs rename to prim_number/CSharp.cs