- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试使用确定性 Miller-Rabin 算法实现素数检查功能,但结果并不总是正确的:在检查前 1,000,000 个数字时,它只找到 78,495 而不是 78,498。
这是使用 [2, 7, 61] 作为基数获得的,根据维基百科,对于最大 4,759,123,141 的值应该始终是正确的。
有趣的是,缺少的 3 个素数正是组成基数的素数(2、7 和 61)。
为什么会这样?我使用的代码如下:
T modular_power(T base, T exponent, T modulo) {
base %= modulo;
T result = 1;
while (exponent > 0) {
if (exponent % 2 == 1)
result = (result * base) % modulo;
base = (base * base) % modulo;
exponent /= 2;
}
return result;
}
bool miller_rabin(const T& n, const vector<T>& witnesses) {
unsigned int s = 0;
T d = n - 1;
while (d % 2 == 0) {
s++;
d /= 2;
}
for (const auto& a : witnesses) {
if (modular_power<T>(a, d, n) == 1)
continue;
bool composite = true;
for (unsigned int r = 0; r < s; r++) {
if (modular_power<T>(a, (T) pow(2, r) * d, n) == n - 1) {
composite = false;
break;
}
}
if (composite)
return false;
}
return true;
}
bool is_prime(const T& n) {
if (n < 4759123141)
return miller_rabin(n, {2, 7, 61});
return false; // will use different base
}
最佳答案
当基数和输入相同时,Miller-Rabin 确实不起作用。在那种情况下发生的是 ad mod n 为零(因为 a mod n 为零,所以这实际上是将零提高到一些不相关的幂),并且算法的其余部分无法“从零开始“逃逸”,并得出结论,您正在处理复合 Material 。
作为一个特例,Miller-Rabin 从不使用输入 2,因为没有可以选择的基数。 2本身没用,1也没用,什么都不剩。
关于c++ - 确定性 Miller-Rabin 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48242447/
突然遇到两种Miller Rabin素性检验方法。其中一个uses randoms和另一个 does not use randoms . 第二个里面有隐藏的随机生成还是什么?谢谢。 最佳答案 第二个是
我正在实现 Wikipedia's Miller-Rabin algorithm但似乎没有得到甚至模糊恰当的结果。 7, 11, 19, 23 等被报道为复合 Material 。事实上,当 k>12
我很清楚单个 Miller-Rabin 测试以三次对数时间运行。我知道蒙哥马利模幂和 GNFS 并且我不会问任何那些奇特的理论。我想知道的是,在特征硬件(例如,2.2 GHz Opteron 或某某显
我正在尝试制作 RSA 算法。为此,我需要 rabin-miller+witness+modular exponentiation(至少我需要使用它)。当我生成随机数以检查 rabin miller
我有 Miller-Rabin 实现 def MillerRabin(n,a): e = 0 q = n-1 while q % 2 == 0: e +=
我是一名计算机科学专业的学生,我正在自学算法类(class)。 在类(class)中我看到了这个问题: Show an efficient randomized algorithm to fact
我需要每天拆分包含 50K+ 列的巨大 (>1 Gb) CSV 文件。 我找到了Miller作为此类任务的有趣且高性能的工具。 但我被米勒的文档困住了。 如何将一个 CSV 拆分为 N 个较小的 CS
我仍然是一个新手编码员,本着努力提高我的技能的精神,我正在开发一个 Miller-Rabin java 程序,该程序似乎在大多数情况下都能工作。然而,有一些数字会导致程序连续运行(至少几分钟)。 其中
我正在学习 Miller Rabin,我正在查看来自 https://en.wikibooks.org/wiki/Algorithm_Implementation/Mathematics/Primal
Miller-Rabin test使用 k 个随机整数来测试素数。 根据 CLRS,第 3rd 版,第 971 页: Theorem 31.38 If n is an odd composite nu
我是 Scheme 新手。我尝试并使用 PLT Scheme 实现了 Rabin-Miller 算法的概率变体。我知道这是概率性的,但大多数时候我都得到了错误的结果。我已经使用 C 实现了同样的事情,
我最近遇到了这段 Rabin-Miller 算法的代码,如描述的那样 here : from random import randint def _bits_of_n(n):
作为我自己的练习,我正在实现 Miller-Rabin 测试。 (通过 SICP 工作)。我理解费马小定理并且能够成功地实现它。我在 Miller-Rabin 测试中被绊倒的部分是这个“1 mod n
我正在尝试使用确定性 Miller-Rabin 算法实现素数检查功能,但结果并不总是正确的:在检查前 1,000,000 个数字时,它只找到 78,495 而不是 78,498。 这是使用 [2, 7
我知道 Miller–Rabin primality test是概率的。但是我想将它用于 programming task没有错误的余地。 如果输入数字是 64 位整数(即 C 中的 long lon
我正在为 Diffie-Hellman 类型的 key p 生成一个 2048 位安全素数,使得 p 和 (p-1)/2 都是素数。 我可以在 p 和 (p-1)/2 上使用多少次 Rabin-Mil
欢迎。我正在尝试实现 MillerRabin 测试以检查给定的大数是否为素数。这是我的代码: public static bool MillerRabinTest(BigInteger number
我正在尝试使用 Park&Miller RNG ran0 1 2 3(3 目前不起作用)在 C++ 中生成伪随机数,来自“C 中的数值配方”。这些生成器可以正常工作,因为它使样本均匀分布在 0 和 1
我一直在尝试实现 the algorithm from wikipedia虽然它从不将合数输出为质数,但它会将大约 75% 的质数输出为合数。 最多 1000 它为我提供了这个素数输出: 3, 5,
我已经实现了 Miller-Rabin 素数测试,并且每个函数似乎都可以单独正常工作。但是,当我尝试通过生成 70 位随机数来找到素数时,我的程序在找到通过 Miller-Rabin 测试(10 步)
我是一名优秀的程序员,十分优秀!