public bool IsProbablePrime(BigInteger source) {
int certainty = 2;
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;
}
RandomNumberGenerator rng = RandomNumberGenerator.Create();
byte[] bytes = new byte[source.ToByteArray().LongLength];
BigInteger a;
for (int i = 0; i < certainty; i++) {
do {
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;
}原文:http://www.cnblogs.com/tmywu/archive/2013/05/15/3079403.html
分享一个C#的Rabin-Miller素性检验算法
最新推荐文章于 2024-06-24 16:13:10 发布
本文介绍了一种使用Miller-Rabin素性测试算法的实现方法,该方法通过随机选取多个基数进行测试来判断一个大整数是否为素数。文章提供了一个具体的C#实现示例,并解释了算法的工作原理。

276

被折叠的 条评论
为什么被折叠?



