-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPollard Rho ( probabilistic Factorization) Algorithm.cpp
More file actions
84 lines (52 loc) · 1.74 KB
/
Pollard Rho ( probabilistic Factorization) Algorithm.cpp
File metadata and controls
84 lines (52 loc) · 1.74 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//This is an algorithm For Finding a Divisor of a Number . It is a probabilistic algorithm .
//Using BirthDay paradox to explain the probability
//And its is a good algorthm for finding a divisior of a Large Number such as N >= 1e15 [ which requres less than 1e6 moves ]
[There is a 90% plus probability that using 1e6 Random number we shall get a divisor]
int cnt = 0;
int Pollard_Rho(int N)
{
if(N==1)
{
return N;
}
if(N%2==0)
{
return 2;
}
srand(time(NULL));
int x = (rand()%(N-2))+2;
int y = x;
int d = 1;
int c = (rand()%(N-1))+1;
while(d==1)
{
//Tortoise : Slow pointer
x = (((x%N)*(x%N))%N + c)%N;
//Hare : Fast Pointer
y = (((y%N)*(y%N))%N + c)%N;
y = (((y%N)*(y%N))%N + c)%N;
d = __gcd( abs(x-y) , N);
++cnt;
if(d==N) // x mod N = y mod N . which means [ abs(x-y) = N ] eta hole Notun kore abar algorithm chalate hobe
{
//Mane Pollard rho loop e pore gese
//Notun iteration dorkar hobe
return Pollard_Rho(N);
}
}
return d;
}
int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int N = 223007;
N = N*N*N;
//cin>>N;
int d = Pollard_Rho(N);
cout<<"The Number : "<<N<<endl;
cout<<"Number of Move : "<<cnt<<endl;
cout<<"Divisor: "<<d<<endl;
cout<<"Confirmation : "<<(N%d)<<endl;
return 0;
}