有谁能帮我完成pollard rho的实现吗我已经在C语言中实现了这一点。对于10位以上的数字,它可以正常工作,但不能处理更大的数字。
请帮助我改进它,以便对18位以下的数字进行因式分解我的代码是this:
#include<stdio.h>
#include<math.h>
int gcd(int a, int b)
{
if(b==0) return a ;
else
return(gcd(b,a%b)) ;
}
long long int mod(long long int a , long long int b , long long int n )
{
long long int x=1 , y=a ;
while(b>0)
{
if(b%2==1) x = ((x%n)*(y%n))%n ;
y = ((y%n)*(y%n))%n ;
b/=2 ;
}
return x%n ;
}
int isprimes(long long int u)
{
if(u==3)
return 1 ;
int a = 2 , i ;
long long int k , t = 0 , r , p ;
k = u-1 ;
while(k%2==0)
{ k/=2 ; t++ ; }
while(a<=3) /*der are no strong pseudoprimes common in base 2 and base 3*/
{
r = mod(a,k,u) ;
for(i = 1 ; i<=t ; i++)
{
p = ((r%u)*(r%u))%u ;
if((p==1)&&(r!=1)&&(r!=(u-1)))
{ return 0 ; }
r = p ;
}
if(p!=1)
return 0 ;
else
a++ ;
}
if(a==4)
return 1 ;
}
long long int pol(long long int u)
{
long long int x = 2 , k , i , a , y , c , s;
int d = 1 ;
k = 2 ;
i = 1 ;
y = x ;
a = u ;
if(isprimes(u)==1)
{
return 1;
}
c=-1 ;
s = 2 ;
while(1)
{
i++;
x=((x%u)*(x%u)-1)% u ;
d = gcd(abs(y-x),u) ;
if(d!=1&&d!=u)
{ printf("%d ",d);
while(a%d==0) { a=a/d; }
x = 2 ;
k = 2 ;
i = 1 ;
y = x ;
if(a==1)
{ return 0 ; }
if(isprimes(a)!=0)
{ return a ; }
u=a ;
}
if(i==k)
{y = x ; k*=2 ; c = x ;} /*floyd cycle detection*/
if(c==x)
{ x = ++s ; }
}
return ;
}
int main()
{
long long int t ;
long long int i , n , j , k , a , b , u ;
while(scanf("%lld",&n)&&n!=0)
{ u = n ; k = 0 ;
while(u%2==0)
{ u/=2 ; k = 1 ; }
if(k==1) printf("2 ") ;
if(u!=1)
t = pol(u) ;
if(u!=1)
{
if(t==1)
{ printf("%lld",u) ; }
else
if(t!=0)
{ printf("%lld",t) ; }
}
printf("\n");
}
return 0;
}
抱歉,代码太长我是个新的编码员。
最佳答案
当你把两个数相乘成模m
时,中间产物几乎可以变成m^2
。因此,如果使用64位无符号整数类型,它可以处理的最大模量为2^32
,如果模数较大,则可能发生溢出。当模数稍微大一点的时候,这是很少见的,但这只会使它变得不那么明显,如果模数允许溢出的可能性,你就不能依靠运气。
如果选择一个代表绝对值模m
的残数类,最多可选择m/2
或类似的值,则可以获得2倍的较大范围:
uint64_t mod_mul(uint64_t x, uint64_t y, uint64_t m)
{
int neg = 0;
// if x is too large, choose m-x and note that we need one negation for that at the end
if (x > m/2) {
x = m - x;
neg = !neg;
}
// if y is too large, choose m-y and note that we need one negation for that at the end
if (y > m/2) {
y = m - y;
neg = !neg;
}
uint64_t prod = (x * y) % m;
// if we had negated _one_ factor, and the product isn't 0 (mod m), negate
if (neg && prod) {
prod = m - prod;
}
return prod;
}
因此,这将允许使用64位无符号类型的模
2^33
。一步也不算大。解决这个问题的建议是使用一个大的整数库,例如gmp可以作为一个发行包在大多数(如果不是所有的话)linux发行版上使用,并且(相对地)可以在windows上轻松安装。
如果这不是一个选择(真的,你确定吗?),您可以使用俄语农民乘法使其适用于更大的模(对于无符号64位整数类型,可达
2^63
):x * y = 2 * (x * (y/2)) + (x * (y % 2))
因此,对于计算,只需要
2*(m-1)
不溢出即可。uint64_t mod_mult(uint64_t x, uint64_t y, uint64_t m)
{
if (y == 0) return 0;
if (y == 1) return x % m;
uint64_t temp = mod_mult(x,y/2,m);
temp = (2*temp) % m;
if (y % 2 == 1) {
temp = (temp + x) % m;
}
return temp;
}
但是请注意,这个算法需要o(log y)个步骤,因此在实践中相当慢。对于较小的
m
可以加快它的速度,如果2^k*(m-1)
不溢出,可以按k
位而不是单位(x*y = ((x * (y >> k)) << k) + (x * (y & ((1 << k)-1)))
)的步骤进行,这是一个很好的改进,如果你的模永远不大于48或56位,比如说。使用模块乘法的变体,你的算法将适用于更大的数字(但速度会慢得多)。您还可以尝试测试模量的大小和/或确定使用哪种方法的系数,如果
m < 2^32
或x < (2^64-1)/y
,则简单的(x * y) % m
即可。关于c - Pollard Rho因式分解方法在C中的实现,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10862358/