-
Notifications
You must be signed in to change notification settings - Fork 0
/
Help Ishaan.cpp
61 lines (53 loc) · 1.15 KB
/
Help Ishaan.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
//{ Driver Code Starts
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
bool isPrime(int num){
for(int i=2;i<=sqrt(num);i++){
if(num%i==0){
return false;
}
}
return true;
}
int NthTerm(int N){
int diff1=INT_MAX;
int diff2=INT_MAX;
for(int i=N;i>=2;i--){
if(isPrime(i)){
//cout<<n-i<<endl;
diff1=N-i;
break;
}
}
for(int i=N+1;i>N;i++){
if(isPrime(i)){
//cout<<i-n<<endl;
diff2=i-N;
break;
}
}
if(diff1<=diff2){
return diff1;
}
else{
return diff2;
}
}
};
//{ Driver Code Starts.
int main(){
int tc;
cin >> tc;
while(tc--){
int N;
cin >> N;
Solution obj;
int ans = obj.NthTerm(N);
cout << ans << "\n";
}
return 0;
}
// } Driver Code Ends