-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmiller_rabin.cpp
75 lines (57 loc) · 1.1 KB
/
miller_rabin.cpp
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
#include <bits/stdc++.h>
#define EPOCHS 40
#define even(a) (!(a&1))
typedef long long ll;
using namespace std;
/**
* Exponentiation function rewritten using
* binary exponenetiation for computations in
* log(n) time
*/
ll power(ll a, ll b, ll p) {
ll result = 1;
while(b!=0) {
if(!even(b)) {
result = ((result%p) * (a%p))%p;
}
a = ((a%p)*(a%p))%p;
b = b>>1;
}
return result;
}
bool primality_test(ll a, ll n, ll p) {
if(even(n)) {
ll x = power(a, (ll)n/2, p)-1;
ll y = power(a, (ll)n/2, p)+1;
if(x%p==0 || y%p==0) {
return true;
}
if(even((ll)n/2)) {
return primality_test(a, (ll)n/2, p);
}
else {
return false;
}
}
return true;
}
bool miller_rabin(ll n) {
for(int epochs=0; epochs<EPOCHS; ++epochs) {
ll a=(rand()%(n-1))+1;
if(power(a, n-1, n)-1 != 0) {
return false;
}
else if(!primality_test(a, n-1, n)){
return false;
}
}
return true;
}
int main() {
srand(time(0));
ll n;
printf("Enter the number: ");
scanf("%lld", &n);
(miller_rabin(n))?printf("%lld is prime!\n", n):printf("%lld is composite.\n", n);
return 0;
}