Pollard rho 整数分解
我正在尝试在 C/C++ 中实现 Pollard Rho 整数分解。Google 为我提供了问题的 Java 实现 此处。
我不太了解 Java,所以我想出了这个。我在 C++ 中的实现适用于大多数人但很少有像我在那里使用的“9999”这样的情况。
我知道 C++ 没有 Biginteger 类,所以我无法拥有 JAVA 中提供的完整功能,但我想分解 15 位数字,这足以满足 unsigned long long
请指出什么我的实施错误。
I am trying to implement Pollard Rho integer factorization in C/C++.Google gives me a Java implementation of the problem here.
I don't know Java that well,so what I came up with this.My implemenation in C++ works for most cases but not in few like the one "9999", I used there.
I am aware that C++ didn't have Biginteger class so I can't have the full functionality as it gives in JAVA but I want to factorize 15 digits numbers that's sufficient for unsigned long long
Please point out what wrong in my implementation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题就在这里:
您的
abs
宏中缺少一些括号。尝试:相反。 (考虑在
x-xx <= 0
的情况下扩展abs(x-xx)
时会发生什么。)另外,为什么你的 gcd 函数返回一个 int 而不是比 BigInteger?
您还应该注意(假设 unsigned long long 是 64 位整数类型)此代码对于大于
2**32
的N
无法正常工作:如果x
(或xx
)大于或等于2**32
,则x*x
将换行模2**64
,为您提供了错误的x*x % N
值。The problem's right here:
You're missing some parentheses in your
abs
macro. Try:instead. (Consider what happens when
abs(x-xx)
is expanded in the casex-xx <= 0
.)Also, why does your gcd function return an int rather than a BigInteger?
You should also be aware that (assuming unsigned long long is a 64-bit integer type) this code won't work correctly for
N
larger than2**32
: ifx
(orxx
) is greater than or equal to2**32
thenx*x
will wrap modulo2**64
, giving you the wrong value forx*x % N
.我发现了一个区别:Java 代码将
c
和x
分配为new BigInteger(N.bitLength(), random)
,而C++代码使用rand() % N
,这是一个较小的随机范围。对于值 9999,二进制为 10011100001111,因此 Java 代码将为c
和x
提供最大值 16383。I've spotted one difference: the Java code assigns
c
andx
to benew BigInteger(N.bitLength(), random)
, whereas the C++ code usesrand() % N
, which is a smaller random range. For the value 9999, the binary is 10011100001111, so the Java code will givec
andx
a maximum value of 16383.您可以尝试 Pollard Rho 的约 100 行 C 实现:
有一些助手:
有一个必需的质数检查器:
有两个因式分解函数:
有一个因式分解< strong>manager:
有一个main:
尝试一下:
这里是来源 了解更多信息,谢谢。
You can try this ~100 lines C implementation of Pollard Rho :
There is some helpers :
There is a required prime checker :
There is two factorization functions :
There is a factorizarion manager :
There is a main :
To try it :
Here is the source for more informations, Thank You.