-
Notifications
You must be signed in to change notification settings - Fork 1
/
prime_no.cpp
171 lines (147 loc) · 2.81 KB
/
prime_no.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <bits/stdc++.h>
using namespace std;
string add_one(string s){
reverse(s.begin(),s.end());
int n=s.size();
int carry=0;
int sum=(s[0]-'0')+1;
carry=sum/10;
s[0]=(sum%10+'0');
for(int i=1;i<n;i++){
if(carry==0) {reverse(s.begin(),s.end()); return s;}
else{
sum=(s[i]-'0')+carry;
s[i]=(sum%10+'0');
carry=sum/10;
}
}
if(carry) s+=(carry+'0');
reverse(s.begin(),s.end());
return s;
}
bool Smaller(string s1, string s2){
int n1 = s1.length(), n2 = s2.length();
if (n1 < n2)
return true;
if (n2 < n1)
return false;
for (int i=0; i<n1; i++)
if (s1[i] < s2[i])
return true;
else if (s1[i] > s2[i])
return false;
return false;
}
bool small2(string s1, string s2){
int n1 = s1.length(), n2 = s2.length();
if (n1 < n2)
return true;
if (n2 < n1)
return false;
for (int i=0; i<n1; i++)
if (s1[i] < s2[i])
return true;
else if (s1[i] > s2[i])
return false;
return true;
}
string Diff(string s1, string s2){
if (Smaller(s1, s2))
swap(s1, s2);
string str = "";
int n1 = s1.length(), n2 = s2.length();
reverse(s1.begin(), s1.end());
reverse(s2.begin(), s2.end());
int carry = 0;
for (int i=0; i<n2; i++){
int sub = ((s1[i]-'0')-(s2[i]-'0')-carry);
if (sub < 0){
sub = sub + 10;
carry = 1;
}
else
carry = 0;
str.push_back(sub + '0');
}
for (int i=n2; i<n1; i++){
int sub = ((s1[i]-'0') - carry);
if (sub < 0){
sub = sub + 10;
carry = 1;
}
else
carry = 0;
str.push_back(sub + '0');
}
reverse(str.begin(), str.end());
return str;
}
string division(string a , string b){
string ans="";
string zero="0";
if(Smaller(a,b)) {return zero;}
string r=a.substr(0,b.size());
if(Smaller(r,b)) {r+=a[b.size()];}
int S=r.size();
while(small2(b,r)){
int i=0;
while(small2(b,r)){
r=Diff(b,r);
int count=0;
for(int j=0;j<r.size();j++){
if(r[j]!='0') break;
else count++;
}
if(count<r.size()) {r=r.substr(count,r.size()-count);}
else{ r="";}
i++;
}
ans+=(i+'0');
if(S>=a.size()) break;
r+=a[S];
S++;
int count=0;
for(int j=0;j<r.size();j++){
if(r[j]!='0') break;
else count++;
}
if(count<r.size()) r=r.substr(count,r.size()-count);
else r="";
while(Smaller(r,b) && S<a.size()){
r+=a[S];
S++;
ans+='0';
int count=0;
for(int j=0;j<r.size();j++){
if(r[j]!='0') break;
else count++;
}
if(count<r.size()) r=r.substr(count,r.size()-count);
else r="";
}
}
if(r.size()==0) r="0";
return r;
}
bool is_prime(string a){
int n=a.size();
string b="2";
while(b.size()<=(n/2+1)&& Smaller(b,a)){
string r=division(a,b);
if(r=="0") return false;
b=add_one(b);
}
return true;
}
int main(){
int T;
cin>>T;
while(T--){
string a;
cin>>a;
if(a=="1" ) cout<<"Not a Prime"<<endl;
if(is_prime(a)) cout<<"Prime"<<endl;
else cout<<"Not a Prime"<<endl;
}
return 0;
}