Pollard Rho 实施有什么问题
#include <iostream>
#include <cstdlib>
typedef unsigned long long int ULL;
ULL gcd(ULL a, ULL b)
{
for(; b >0 ;)
{
ULL rem = a % b;
a = b;
b = rem;
}
return a;
}
void pollard_rho(ULL n)
{
ULL i = 0,y,k,d;
ULL *x = new ULL[2*n];
x[0] = rand() % n;
y = x[0];
k = 2;
while(1){
i = i+1;
std::cout << x[i-1];
x[i] = (x[i-1]*x[i-1]-1)%n;
d = gcd(abs(y - x[i]),n);
if(d!= 1 && d!=n)
std::cout <<d<<std::endl;
if(i+1==k){
y = x[i];
k = 2*k;
}
}
}
int main()
{
srand(time(NULL));
pollard_rho(10);
}
该实现源自 CLRS 第二版(页码 894)。 while(1)
对我来说看起来很可疑。 while 循环的终止条件应该是什么?
我尝试了 k<=n
但这似乎不起作用。我遇到分段错误。代码中有什么缺陷以及如何纠正它?
#include <iostream>
#include <cstdlib>
typedef unsigned long long int ULL;
ULL gcd(ULL a, ULL b)
{
for(; b >0 ;)
{
ULL rem = a % b;
a = b;
b = rem;
}
return a;
}
void pollard_rho(ULL n)
{
ULL i = 0,y,k,d;
ULL *x = new ULL[2*n];
x[0] = rand() % n;
y = x[0];
k = 2;
while(1){
i = i+1;
std::cout << x[i-1];
x[i] = (x[i-1]*x[i-1]-1)%n;
d = gcd(abs(y - x[i]),n);
if(d!= 1 && d!=n)
std::cout <<d<<std::endl;
if(i+1==k){
y = x[i];
k = 2*k;
}
}
}
int main()
{
srand(time(NULL));
pollard_rho(10);
}
This implementation is derived from CLRS 2nd edition (Page number 894). while(1)
looks suspicious to me. What should be the termination condition for the while loop?
I tried k<=n
but that doesn't seem to work. I get segmentation fault. What is the flaw in the code and how to correct it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我只有 CLRS 第一版,但假设它与第二版没有太大不同,终止条件的答案在下一页:
因此,从技术上讲,CLRS 中的演示文稿没有终止条件(这可能就是为什么他们称其为“启发式”和“过程”而不是“算法”),并且不能保证它实际上会产生任何内容有用。实际上,您可能希望根据预期的 n1/4 更新设置一些迭代限制。
I only have a 1st edition of CLRS, but assuming it's not too different from the 2nd ed., the answer to the termination condition is on the next page:
So, technically speaking, the presentation in CLRS doesn't have a termination condition (that's probably why they call it a "heuristic" and "procedure" rather than an "algorithm") and there are no guarantees that it will ever actually produce anything useful. In practice, you'd likely want to put some iteration bound based on the expected n1/4 updates.
为什么要存储所有这些中间值?您确实不需要将 x 和 y 放入数组中。只需使用您不断重复使用的 2 个变量:
x
和y
。另外,将
while(1)
替换为while(d == 1)
并在之前切断循环。您的循环应该成为
因此,如果您传递循环内使用的函数, 额外的分数作为
pollard
的参数,这样如果它无法用一个函数找到结果,它就会尝试另一个函数。Why store all those intermediary values? You really don't need to put x and y in a array. Just use 2 variables which you keep reusing,
x
andy
.Also, replace
while(1)
withwhile(d == 1)
and cut the loop beforeSo your loop should become
Extra points if you pass the function used inside the loop as a parameter to
pollard
, so that if it can't find the result with one function, it tries another.尝试将
while(1) { i = i + 1;
替换为:Try replacing
while(1) { i = i + 1;
with this: