1. 程式人生 > >pollard_rho 演算法進行質因數分解

pollard_rho 演算法進行質因數分解

//************************************************
//pollard_rho 演算法進行質因數分解
//************************************************
long long factor[100];//質因數分解結果(剛返回時是無序的)
int tol;//質因數的個數。陣列小標從0開始

long long gcd(long long a,long long b)
{
    if(a==0)return 1;
    if(a<0) return gcd(-a,b);
    while(b)
    {
        
long long t=a%b; a=b; b=t; } return a; } long long Pollard_rho(long long x,long long c) { long long i=1,k=2; long long x0=rand()%x; long long y=x0; while(1) { i++; x0=(mult_mod(x0,x0,x)+c)%x; long long d=gcd(y-x0,x); if(d!=1&&d!=x) return
d; if(y==x0) return x; if(i==k){y=x0;k+=k;} } } //對n進行素因子分解 void findfac(long long n) { if(Miller_Rabin(n))//素數 { factor[tol++]=n; return; } long long p=n; while(p>=n)p=Pollard_rho(p,rand()%(n-1)+1); findfac(p); findfac(n/p); }