Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Pollard Rho算法陷入循环_Java_Algorithm_Loops_Factorization - Fatal编程技术网

Java Pollard Rho算法陷入循环

Java Pollard Rho算法陷入循环,java,algorithm,loops,factorization,Java,Algorithm,Loops,Factorization,我正在尝试实现Pollard的Rho算法来查找整数n的因子。我有一个通常有效的实现,但现在有一些问题。在这种情况下,my n=262063 这是我的Rho算法,以及引用的getGCD()和pollardRhoFunction()[我保留了println,以便以后提供结果]: public static int pollardRho(int n, int xStart){ // the function we will be using is f(x)=x^2+1, as per our

我正在尝试实现Pollard的Rho算法来查找整数n的因子。我有一个通常有效的实现,但现在有一些问题。在这种情况下,my n=262063

这是我的Rho算法,以及引用的
getGCD()
pollardRhoFunction()
[我保留了println,以便以后提供结果]:

public static int pollardRho(int n, int xStart){

    // the function we will be using is f(x)=x^2+1, as per our textbook,            
    // (in question# 5.26)

    int x = xStart;
    int xPrime = pollardRhoFunction(x) % n;
    int p = getGCD(x-xPrime, n);
    int nmbr = 0;
    System.out.println("x="+x);
    System.out.println("xPrime="+xPrime);
    System.out.println("p"+p);

    while (p == 1||p==31313||p==20||p==75||p==25||p == n ||p==262063){
        System.out.println("i="+nmbr);
        x = pollardRhoFunction(x) % n;
        xPrime = pollardRhoFunction(xPrime) % n;
        System.out.println("xPrime="+xPrime);
        xPrime = pollardRhoFunction(xPrime) % n;
        System.out.println("xPrime="+xPrime);
        p = getGCD(x-xPrime, n);
        nmbr++;
    }
    updateIterNmbrs(nmbr);
    if (p == n){
        return -1;
    }
    else{
        return p;
    }
}
public static int getGCD(int a, int b){
int a_0 = a;
int b_0 = b;
int q = (int)Math.floor(a_0/b_0); 
int r = a_0 - q*b_0;
while (r > 0){
    a_0 = b_0;
    b_0 = r;
    q = (int)Math.floor(a_0/b_0);
    r = a_0 - q*b_0;    
} 

r = b_0;
return r;
}

public static int pollardRhoFunction(int x){

    // the function we will be using is f(x)=x^2+1, as per our textbook,            
    // (in question# 5.26)

    int result = (int)Math.pow(x,2)+2;
    //System.out.println("x^2+1="+result);
    return result;
}
过了一会儿,它就陷入了一个循环。以下是System.out.println()的(部分)打印输出:


你明白了……由于某种原因,它被卡在了某个点上……

你似乎在这一行有一个错误:

int result = (int)Math.pow(x,2)+2;
这应该是

int result = x*x + 1;

它仍然会卡住…最初是这样的,然后我改变了它,试图解决这个问题。显然,它没有……事实上,你是对的!我以为你的更正是“+1”而不是“+2”。但你是对的,是math.pow造成了这个问题。你能解释一下为什么它不起作用吗?Math.pow()函数接受两个双精度数字,而不是整数。
int result = x*x + 1;