-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMillerRabin.cpp
More file actions
66 lines (60 loc) · 1.21 KB
/
MillerRabin.cpp
File metadata and controls
66 lines (60 loc) · 1.21 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
#include <iostream>
#include <cmath>
using namespace std;
unsigned long long int anp(unsigned long long int a, unsigned long long int n, unsigned long long int p) {
unsigned long long int b = 1;
a%=p;
while (n--) {
b = (b * a) % p;
}
return b;
}
int main() {
unsigned long long int n, s, k; //n=2^s*k+1
s = 0;
// cin >> n;
n=114553;
if (n % 2 == 0 && n > 2) {
cout << "nÊǺÏÊý";
}
unsigned long long int temp = n - 1;
while (temp % 2 == 0) {
temp /= 2;
s++;
}
k = temp;
cout << "s=" << s << ", k=" << k << endl;
unsigned long long int t;
// cin >> t;
t=10;
unsigned long long int flag=0;
while (t--) {
unsigned long long int random = rand() % (n - 3) + 2;
cout << "random=" << random << endl;
unsigned long long int r;
r = anp(random, k, n);
cout << "r=" << r << endl;
if ((r+n-1)%n==0||(r+1)%n==0) {
flag=1;
cout<<"phase1:r="<<r<<"\n";
continue;
}
flag = 0;
unsigned long long int i = 0;
while (i < s - 1) {
i++;
r = (r*r)%n;
cout<<"phase2:r="<<r<<"\n";
if ((r+1)%n==0) {
flag = 1;
cout<<"phase2:i="<<i<<"\n";
break;
}
}
if (flag==0)break;
}
if(flag==1) {cout<<"nÊÇËØÊý\n";}
else {cout<<"nÊǺÏÊý\n";}
system("pause");
return 0;
}