Init Primes

This commit is contained in:
BlubbFish 2019-02-11 16:44:28 +00:00
commit 0eeb056458
4 changed files with 364 additions and 0 deletions

20
Primes.sln Normal file
View File

@ -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

58
Primes/Primes.csproj Normal file
View File

@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{1933EC8F-A012-4176-A3E7-55487F4B183F}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Primes</RootNamespace>
<AssemblyName>Primes</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

250
Primes/Program.cs Normal file
View File

@ -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;
}
}
}

View File

@ -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")]