commit 0eeb0564581023e770870a613b833bc30e0acffa Author: BlubbFish Date: Mon Feb 11 16:44:28 2019 +0000 Init Primes diff --git a/Primes.sln b/Primes.sln new file mode 100644 index 0000000..d90beda --- /dev/null +++ b/Primes.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2012 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Primes", "Primes\Primes.csproj", "{1933EC8F-A012-4176-A3E7-55487F4B183F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {1933EC8F-A012-4176-A3E7-55487F4B183F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1933EC8F-A012-4176-A3E7-55487F4B183F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1933EC8F-A012-4176-A3E7-55487F4B183F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1933EC8F-A012-4176-A3E7-55487F4B183F}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Primes/Primes.csproj b/Primes/Primes.csproj new file mode 100644 index 0000000..b4a61d6 --- /dev/null +++ b/Primes/Primes.csproj @@ -0,0 +1,58 @@ + + + + + Debug + AnyCPU + {1933EC8F-A012-4176-A3E7-55487F4B183F} + Exe + Properties + Primes + Primes + v4.5 + 512 + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Primes/Program.cs b/Primes/Program.cs new file mode 100644 index 0000000..95ec107 --- /dev/null +++ b/Primes/Program.cs @@ -0,0 +1,250 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Primes { + class Program { + static UInt64 pri = 600851475143; + static int num_prim_sde = 0; + static int num_prim_wilson = 0; + static int num_prim_fermat = 0; + static int num_prim_rab = 0; + static void Main(string[] args) { + /*Console.WriteLine("Primzahlentests: "); + int max = 1000; + int min = 2; + + Console.WriteLine("Sieb des Erastoteles von " + min + " bis " + max + ":"); + DateTime now = DateTime.Now; + createSieb(max); + for(int i = min; i <= max; i++) { + if(i % 2 == 0 && i != 2) { + continue; + } + siebdeserastoteles(i); + } + DateTime end = DateTime.Now; + Console.WriteLine("Zeit von " + now.ToLongTimeString() + " bis " + end.ToLongTimeString() + ": " + (end - now).TotalMilliseconds + " ms\n"); + + Console.WriteLine("Wilson-Test von " + min + " bis " + max + ":"); + now = DateTime.Now; + for(int i = min; i <= max; i++) { + if(i % 2 == 0 && i != 2) { + continue; + } + wilsontest(i); + } + end = DateTime.Now; + Console.WriteLine("Zeit von " + now.ToLongTimeString() + " bis " + end.ToLongTimeString() + ": " + (end - now).TotalMilliseconds + " ms\n"); + + Console.WriteLine("Fermat-Test mit 60 Runden von " + min + " bis " + max + ":"); + now = DateTime.Now; + for(int i = min; i <= max; i++) { + if(i % 2 == 0 && i != 2) { + continue; + } + fermattest(i); + } + end = DateTime.Now; + Console.WriteLine("Zeit von " + now.ToLongTimeString() + " bis " + end.ToLongTimeString() + ": " + (end - now).TotalMilliseconds + " ms\n"); + + Console.WriteLine("Miller-Rabin-Test mit 60 Runden von " + min + " bis " + max + ":"); + now = DateTime.Now; + for(int i = min; i <= max; i++) { + if(i % 2 == 0 && i != 2) { + continue; + } + mrtest(i); + } + end = DateTime.Now; + Console.WriteLine("Zeit von " + now.ToLongTimeString() + " bis " + end.ToLongTimeString() + ": " + (end - now).TotalMilliseconds + " ms\n"); + + Console.WriteLine("\n\n# Primzahlen bis " + max + " Sieb: " + num_prim_sde + " Wilson: " + num_prim_wilson + + " Fermat: " + num_prim_fermat + " MilRab: " + num_prim_rab); */ + //MillerRabinTest(pri, true); + //MillerRabinTest(65, true); + //Console.WriteLine("c=1415 d=7467, n=11413 m="+PotenzMod(1415, 7467, 11413)+" m=c^d(m)"); + Console.ReadLine(); + } + + private static void MillerRabinTest(UInt64 m, Boolean debug =false) { + if(m == 2 || m == 3) { + if(debug) { + Console.Write("RillerRab: prim? "); + } + + num_prim_rab++; + return; + } + Boolean prim = true; + UInt64 s = 0; + for(UInt64 r = 1; r < m; r++) { + UInt64 st = PotenzMod(2, r, m + 5); + if((m - 1) % st == 0) { + s = r; + } else { + break; + } + } + UInt64 d = (m - 1) / PotenzMod(2, s, m + 5); + if(debug) { + Console.WriteLine("s=" + s + " d=" + d); + } + + Random ar = new Random(); + for(UInt64 i = 2; i <= m - 2; i++) { + //prim = true; + UInt64 a = i; //(UInt64)ar.Next(2, Int32.MaxValue < m - 2 ? Int32.MaxValue : (Int32)(m - 2)); + if(debug) { + Console.Write("B-Seq mit b=" + a + ": <"); + } + + if (EUKLID1(a, m) != 1) { + if(debug) { + Console.WriteLine("> nullteiler!"); + } + continue; + } + UInt64 b = PotenzMod(a, d, m); + if(debug) { + Console.Write(((b == m - 1) ? "-1" : b.ToString()) + ","); + } + + if (b % m == 1 || b % m == m - 1) { + if(debug) { + Console.WriteLine("> prim?"); + } + + continue; + } + Boolean toend = true; + for(UInt64 r = 1; r < s; r++) { + b = PotenzMod(b, 2, m); + if(debug) { + Console.Write(((b == m - 1) ? "-1" : b.ToString()) + ","); + } + + if (b % m == m - 1) { + toend = false; + break; + } + if(b % m == 1) { + prim = false; + break; + } + } + if(prim && !toend) { + if(debug) { + Console.WriteLine("> prim?"); + } + + continue; + } + prim = false; + if(debug) { + Console.WriteLine("> nicht prim!"); + } + + break; + } + if(debug) { + Console.WriteLine("RillerRab: " + ((prim) ? "prim? \n" : "not prim \n")); + } + + if (prim) { + num_prim_rab++; + } + } + + /*private static void fermattest(int k) { + int fermat = 0; + Random r = new Random(); + for(int i = 0; i <= 60; i++) { + int a = r.Next(k * (-1), k); + if(a == 0) { + continue; + } + if(EUKLID1(a, k) != 1) { + continue; + } + fermat = PotenzMod(a, k - 1, k) % k; + if(fermat != 1) { + fermat = -1; + break; + } + } + //Console.Write("Fermat: " + ((fermat == 1 || k == 2) ? "prim? " : "not prim ")); + if(fermat == 1 || k == 2) { + num_prim_fermat++; + } + }*/ + + static public UInt64 EUKLID1(UInt64 a, UInt64 b) { + if(b > a) { + UInt64 tmp = a; + a = b; + b = tmp; + } + UInt64 dividend = a; + UInt64 divisor = b; + UInt64 rest = dividend % divisor; + while(rest > 0) { + dividend = divisor; + divisor = rest; + rest = dividend % divisor; + } + return divisor; + } + + private static void wilsontest(int p) { + int wilson = FactorialMod(p - 2, p) % p; + //Console.Write("Wilson: " + ((wilson == 1)?"prim ":"not prim ")); + if(wilson == 1) { + num_prim_wilson++; + } + } + + private static void createSieb(int max) { + sieb = new bool[max + 1]; + for(int i = 2; i <= max; i++) { + sieb[i] = true; + } + for(int i = 2; i <= Math.Floor(Math.Sqrt(max)); i++) { + if(sieb[i]) { + for(int k = i; k <= Math.Floor((double)max / i); k++) { + sieb[i * k] = false; + } + } + } + } + + private static bool[] sieb; + + private static void siebdeserastoteles(int n) { + //Console.Write("Sieb: " + ((sieb[n] == true)?"prim ":"not prim ")); + if(sieb[n]) { + num_prim_sde++; + } + } + + static public int FactorialMod(int n, int mod) { + ulong Result = 1; + for(ulong i = 1; i <= (ulong)n; i++) { + Result *= i; + Result = Result % (ulong)mod; + } + return (int)Result; + } + + static public UInt64 PotenzMod(UInt64 x, UInt64 p, UInt64 mod) { + UInt64 Result = 1; + for(UInt64 i = 1; i <= p; i++) { + Result *= x; + Result = Result % mod; + } + return Result; + } + } +} diff --git a/Primes/Properties/AssemblyInfo.cs b/Primes/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..7c26ccf --- /dev/null +++ b/Primes/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// Allgemeine Informationen über eine Assembly werden über die folgenden +// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern, +// die mit einer Assembly verknüpft sind. +[assembly: AssemblyTitle("Primes")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Primes")] +[assembly: AssemblyCopyright("Copyright © 2015")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Durch Festlegen von ComVisible auf "false" werden die Typen in dieser Assembly unsichtbar +// für COM-Komponenten. Wenn Sie auf einen Typ in dieser Assembly von +// COM zugreifen müssen, legen Sie das ComVisible-Attribut für diesen Typ auf "true" fest. +[assembly: ComVisible(false)] + +// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird +[assembly: Guid("972b369b-6680-4004-b491-a591380efdaf")] + +// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten: +// +// Hauptversion +// Nebenversion +// Buildnummer +// Revision +// +// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern +// übernehmen, indem Sie "*" eingeben: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")]