-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNth_Fortunate_Number.cpp
More file actions
62 lines (55 loc) · 1.22 KB
/
Nth_Fortunate_Number.cpp
File metadata and controls
62 lines (55 loc) · 1.22 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
/*
Given an integer n, find the nth Fortunate Number.
A Fortunate number is the smallest integer m > 1 such that, for a given positive integer n, pn + m is a prime number. Here pn is the product of the first n prime numbers, i.e prime factorials (or primorials) of order n.
*/
#include <bits/stdc++.h>
using namespace std;
#define long long int
#define module 10000000007
bool isPrime(int n){
if(n==0 || n==1 )
return false;
for(int i = 2; i<=sqrt(n); i++){
if(n%i==0){
return false;
}
}
return true;
}
int productOfFirstNprims(int n){
int product = 1;
int j = 2,count=0;
while(1){
if(isPrime(j)){
product=(product*j ) %module;// % 1000000007;
count++;
}
if(count==n)
break;
j++;
}
return product;
}
void findNthFortunateNumber(){
int t;
cin>>t;
while(t-->0){
int n;
cin>>n;
int pn = productOfFirstNprims(n);
int m = 2;
while(true){
if(isPrime(pn+m)){
cout<<m<<endl;
break;
}else{
m++;
}
}
}
}
int main() {
//code
findNthFortunateNumber();
return 0;
}