-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPollardsRho.java
45 lines (40 loc) · 1.09 KB
/
PollardsRho.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.*;
import java.io.*;
public class PollardsRho{
public static void main(String[] args){
int N = 2206637;
findFactor(N);
}
static void findFactor(int N){
int a=2;
boolean factorFound=false;
int x1=a; int x2=a;
//for(;!factorFound; a++){
if(gcd(a,N)!=1) {}//continue;
else{
x1 = fun(x1,N);
x2 = fun(fun(x2,N),N);
int gg = gcd(abs(x2-x1),N);
while(gg==1){
System.out.printf("%d %d\n", x1, x2);
x1 = fun(x1,N);
x2 = fun(fun(x2,N),N);
gg = gcd(abs(x2-x1),N);
}
System.out.printf("%d %d\nGCD=%d", x1, x2,gg);
}
//}
}
static int fun(int x, int N){
long y = (long)x;
return (int)((y*y+1L)%(long)N);
}
static int abs(int a){
if(a<0) return -a;
else return a;
}
static int gcd(int a, int b){
if(b==0) return a;
else return gcd(b,a%b);
}
}