Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,27 @@
|
|||
public static class RabinMiller
|
||||
{
|
||||
public static bool IsPrime(int n, int k)
|
||||
{
|
||||
if ((n < 2) || (n % 2 == 0)) return (n == 2);
|
||||
|
||||
int s = n - 1;
|
||||
while (s % 2 == 0) s >>= 1;
|
||||
|
||||
Random r = new Random();
|
||||
for (int i = 0; i < k; i++)
|
||||
{
|
||||
int a = r.Next(n - 1) + 1;
|
||||
int temp = s;
|
||||
long mod = 1;
|
||||
for (int j = 0; j < temp; ++j) mod = (mod * a) % n;
|
||||
while (temp != n - 1 && mod != 1 && mod != n - 1)
|
||||
{
|
||||
mod = (mod * mod) % n;
|
||||
temp *= 2;
|
||||
}
|
||||
|
||||
if (mod != n - 1 && temp % 2 == 0) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,58 @@
|
|||
// Miller-Rabin primality test as an extension method on the BigInteger type.
|
||||
// Based on the Ruby implementation on this page.
|
||||
public static class BigIntegerExtensions
|
||||
{
|
||||
public static bool IsProbablePrime(this BigInteger source, int certainty)
|
||||
{
|
||||
if(source == 2 || source == 3)
|
||||
return true;
|
||||
if(source < 2 || source % 2 == 0)
|
||||
return false;
|
||||
|
||||
BigInteger d = source - 1;
|
||||
int s = 0;
|
||||
|
||||
while(d % 2 == 0)
|
||||
{
|
||||
d /= 2;
|
||||
s += 1;
|
||||
}
|
||||
|
||||
// There is no built-in method for generating random BigInteger values.
|
||||
// Instead, random BigIntegers are constructed from randomly generated
|
||||
// byte arrays of the same length as the source.
|
||||
RandomNumberGenerator rng = RandomNumberGenerator.Create();
|
||||
byte[] bytes = new byte[source.ToByteArray().LongLength];
|
||||
BigInteger a;
|
||||
|
||||
for(int i = 0; i < certainty; i++)
|
||||
{
|
||||
do
|
||||
{
|
||||
// This may raise an exception in Mono 2.10.8 and earlier.
|
||||
// http://bugzilla.xamarin.com/show_bug.cgi?id=2761
|
||||
rng.GetBytes(bytes);
|
||||
a = new BigInteger(bytes);
|
||||
}
|
||||
while(a < 2 || a >= source - 2);
|
||||
|
||||
BigInteger x = BigInteger.ModPow(a, d, source);
|
||||
if(x == 1 || x == source - 1)
|
||||
continue;
|
||||
|
||||
for(int r = 1; r < s; r++)
|
||||
{
|
||||
x = BigInteger.ModPow(x, 2, source);
|
||||
if(x == 1)
|
||||
return false;
|
||||
if(x == source - 1)
|
||||
break;
|
||||
}
|
||||
|
||||
if(x != source - 1)
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue