-
Notifications
You must be signed in to change notification settings - Fork 0
/
Randomized_Primality_Testing.cpp
91 lines (65 loc) · 1.61 KB
/
Randomized_Primality_Testing.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include<bits/stdc++.h>
#include <iostream>
using namespace std;
int gcd(unsigned long long int A,unsigned long long int N){
int t;
while(N>0){
t = A%N;
A = N;
N = t;
}
return A;
}
int Power(unsigned long long int random, unsigned long long int number,unsigned long long int p){
int res = 1;
while(number>0){
if(number%2 == 1){
res = (res*random) % p;
}
number = number/2;
random = (random*random)%p;
}
return res;
}
int isPrime(unsigned long long int num){
int k = 10;
int flag = 0;
if(num<=1 || num ==4){
flag = 1;
}
if(num == 2 && num==3){
flag = 0;
}
if(num%2 == 0 && num !=2){
flag = 1;
}
else{
while(k>0){
srand(time(0));
//it will generate random number till n-2
// rand(n-p+1) where n = n-2 and p=2 as p can not be equal to 1
int a = 2 + (rand() %(num-4));
if( gcd(a,num) != 1){
flag = 1;
}
if( Power(a,num-1,num) != 1){
flag = 1;
}
k--;
}
}
return flag;
}
int main()
{
unsigned long long int n;
cin>>n;
int result = isPrime(n);
if(result == 1){
cout<<"Number is composite";
}
else{
cout<<"Number is Prime";
}
return 0;
}